Built with Tutify.io

    AIStudyLabs

    Pandas Add or Insert Row to DataFrame

    Pandas Add or Insert Row to DataFrame

    How to add or insert a row to pandas DataFrame? To add or insert a row into a specific position in a pandas DataFrame, you can use the loc indexer. You can use multiple ways of Pandas such as append(), pandas.concat(). In this article, I will explain how to add or insert a row into a DataFrame with more examples.

    Key Points –

    <a id="h-quick-examples-of-adding-row-to-dataframe"></a>

    Quick Examples of Adding Row to DataFrame

    Below are quick examples of inserting/adding rows to DataFrame.

    
    # Quick examples of adding row to dataframe
    
    # Example 1: Add row to dataframe
    list_row = ["Hyperion", 27000, "60days", 2000]
    df.loc[len(df)] = list_row
    
    # Example 2: Insert dict as row to the dataframe 
    # Using DataFrame.append()
    new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
    df2 = df.append(new_row, ignore_index=True)
    
    # Example 3: Add new row to specifig index name
    df2 = df.append(pd.DataFrame([new_row],index=['7'],columns=df.columns))
    
    # Example 4: Append row to the DataFrame
    df2 = df.append(pd.Series(new_row, index=df.columns, name='7'))
    
    # Example 5: Using pandas.concat() 
    # To add a row
    new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
    df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
    
    # Example 6: Add specific row/index name 
    # Using DataFrame.loc[]
    df.loc['7', :] = ['Hive',25000,'45days',2000]
    
    # Example 7: Add row in DataFrame 
    # Using DataFrame.loc[]
    df.loc['7'] = ['Hive',25000,'45days',2000]
    

    To run some examples of adding a row to DataFrame, let’s create a Pandas DataFrame dictionary.

    
    import pandas as pd
    technologies = ({
        'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
        'Fee' :[20000,25000,30000,22000,26000],
        'Duration':['30days','40days','35days','60days','50days'],
        'Discount':[1000,2500,1500,1200,3000]
                   })
    df = pd.DataFrame(technologies)
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-add-row-to-pandas-dataframe"></a>

    Add Row to Pandas DataFrame

    To add or insert a row to an existing DataFrame from a dictionary, you can use the append() method which takes a ignore_index=True parameter to add a dictionary as a row to the DataFrame. If you do not pass this parameter, an error will be returned. However, if you pass this parameter into this function will return the updated DataFrame with the newly added row.

    
    # Insert row to the dataframe 
    # Using DataFrame.append()
    df = pd.DataFrame(technologies)
    new_row = {'Courses':'Hyperion', 
               'Fee':24000, 
               'Duration':'55days', 
               'Discount':1800}
    df2 = df.append(new_row, ignore_index=True)
    print("After adding a new row to DataFrame:\n", df2)
    

    Yields the below output.

    Note that when you used ignore_index=True, it ignores the existing index on the DataFrame and set a new index.

    <a id="h-add-or-insert-list-as-row-to-dataframe"></a>

    Add or Insert List as Row to DataFrame

    To add/insert a list as a row to a DataFrame, you can use the loc[] accessor with the length of the DataFrame as the index for the new row.

    
    # Add list as row to dataframe
    list = ["Hyperion", 24000, "55days", 1800]
    df.loc[len(df)] = list
    print("After adding a new row to DataFrame:\n", df)
    
    # Output:
    # Output:
    # After adding a new row to DataFrame:
    #     Courses    Fee Duration  Discount
    # 0     Spark  20000   30days      1000
    # 1    Hadoop  25000   40days      2500
    # 2    pandas  30000   35days      1500
    # 3      Java  22000   60days      1200
    # 4   Pyspark  26000   50days      3000
    # 7  Hyperion  24000   55days      1800
    

    In the above examples, you define a new row as a list list. Then using df.loc[len(df)]=list, you append the new row to the DataFrame using its length as the index for the new row. Note that when you have a default number index, it automatically increments the index and adds the row at the end of the DataFrame.

    <a id="h-add-row-to-dataframe-with-index-name"></a>

    Add Row to DataFrame with Index Name

    Alternatively, you can also use DataFrame.append() function to add a new row to pandas DataFrame with a custom Index.

    
    # Add new row to specifig index name
    df2 = df.append(pd.DataFrame([new_row],index=['7'],columns=df.columns))
    print("After adding a new row to DataFrame:\n", df2)
    
    # Add row to the DataFrame using append()
    df2 = df.append(pd.Series(new_row, index=df.columns, name='7'))
    print("After adding a new row to DataFrame:\n", df2)
    
    # Output:
    # After adding a new row to DataFrame:
    #    Courses    Fee Duration  Discount
    # 0     Spark  20000   30days      1000
    # 1    Hadoop  25000   40days      2500
    # 2    pandas  30000   35days      1500
    # 3      Java  22000   60days      1200
    # 4   Pyspark  26000   50days      3000
    # 7  Hyperion  24000   55days      1800
    

    <a id="h-add-row-at-the-top-of-dataframe-using-concat"></a>

    Add Row at the Top of DataFrame Using concat()

    You can use pd.concat() to add a row to the first position of the Pandas DataFrame with the index position as 0. The reset_index() function will reset the index on the DataFrame to adjust the indexes on other rows.

    
    # Using pandas.concat() to add a row
    new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
    df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
    print("After adding a new row to DataFrame:\n", df2)
    
    # Output:
    # After adding a new row to DataFrame:
    #     Courses    Fee Duration  Discount
    # 0  Hyperion  24000   55days      1800
    # 1     Spark  20000   30days      1000
    # 2    Hadoop  25000   40days      2500
    # 3    pandas  30000   35days      1500
    # 4      Java  22000   60days      1200
    # 5   Pyspark  26000   50days      3000
    

    <a id="h-add-specific-row-index-name-using-loc"></a>

    Add Specific Row/Index Name Using loc[]

    You can use loc[] to add a row with a specific index name to a DataFrame. Let's add the list as a row to DataFrame at the last index position using DataFrame.loc['Index5',:] function.

    
    # Add specific row/index name using DataFrame.loc[]
    df.loc['7', :] = ['Hive',25000,'45days',2000]
    print("After adding a new row to DataFrame:\n", df)
    
    # Add row in DataFrame using DataFrame.loc[]
    df.loc['7'] = ['Hive',25000,'45days',2000]
    print("After adding a new row to DataFrame:\n", df)
    

    In the above example, we use df.loc['7', :] to specify the row with the index name '7' and assign the values ['Hive', 25000, '45days', 2000] to it. This adds a new row with the index name '7' to the DataFrame df with the specified values.

    
    # Output:
    After adding a new row to DataFrame:
             Courses      Fee Duration  Discount
    0          Spark  20000.0   30days    1000.0
    1         Hadoop  25000.0   40days    2500.0
    2         pandas  30000.0   35days    1500.0
    3           Java  22000.0   60days    1200.0
    4        Pyspark  26000.0   50days    3000.0
    7           Hive  25000.0   45days    2000.0
    

    <a id="h-complete-example"></a>

    Complete Example

    
    import pandas as pd
    technologies = ({
        'Courses':["Spark","Hadoop","pandas","Java","Pyspark"],
        'Fee' :[20000,25000,30000,22000,26000],
        'Duration':['30days','40days','35days','60days','50days'],
        'Discount':[1000,2500,1500,1200,3000]
                   })
    df = pd.DataFrame(technologies)
    print(df)
    
    # Insert row to the dataframe using DataFrame.append()
    df = pd.DataFrame(technologies)
    new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
    df2 = df.append(new_row, ignore_index=True)
    print("After adding a new row to DataFrame:\n", df2)
    
    # Add new row to specifig index name
    new_row = {'Courses':'Oracle','Fee':25000,'Duration':'65days','Discount':2800}
    df2 = df.append(pd.DataFrame([new_row],index=['Index'],columns=df.columns))
    print("After adding a new row to DataFrame:\n", df2)
    
    # Append row to the DataFrame
    df2 = df.append(pd.Series(new_row, index=df.columns, name='Index'))
    print("After adding a new row to DataFrame:\n", df2)
    
    # Using pandas.concat() to add a row
    new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
    df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)
    print("After adding a new row to DataFrame:\n", df2)
    
    # Add specific row/index name using DataFrame.loc[]
    df.loc['Index5', :] = ['Hive',25000,'45days',2000]
    print("After adding a new row to DataFrame:\n", df)
    
    # Add row in DataFrame using DataFrame.loc[]
    df.loc['Index5'] = ['Hive',25000,'45days',2000]
    print("After adding a new row to DataFrame:\n", df)
    

    <a id="h-faq-an-adding"></a>

    FAQ an Adding

    You can add a row to a DataFrame using the append() method or by directly assigning values to a new row.

    append() is a method of the DataFrame class and is used to add a single row to the end of the DataFrame. concat() is a function that can be used to concatenate two or more DataFrames along a specified axis, which can be used to add rows at various positions in the DataFrame.

    DataFrame rows are inherently unordered, so there's no direct way to add a row at a specific position. You can append rows to the end of the DataFrame or insert them in the order you desire, but the physical position of rows within a DataFrame is not typically meaningful.

    You can add a row to a DataFrame with a specific index name in Pandas. You can achieve this by creating a new Pandas Series with the row data and specifying the desired index name.

    <a id="h-conclusion"></a>

    Conclusion

    In conclusion, adding or inserting a row into a Pandas DataFrame is a fundamental operation in data manipulation. Throughout this article, we've explored different techniques to accomplish this task. Whether it's utilizing the loc[] accessor for direct assignment, the concat() function for concatenation, or the append() function for straightforward row addition, Pandas offers versatile tools to suit various needs.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles