• Post author:
  • Post category:Pandas
  • Post last modified:May 9, 2024
  • Reading time:16 mins read
You are currently viewing 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/insert a row to DataFrame with several examples.

Advertisements

Key Points –

  • Pandas provide methods like append() and loc[] to add or insert rows into DataFrames efficiently.
  • The append() function adds rows to the end of the DataFrame, while loc[] allows inserting rows at specific positions.
  • Utilize the loc[] indexer to insert a row at a specific location within the DataFrame, providing the index label and the values for the new row.
  • With loc[], you can specify the index where the new row should be inserted.

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 Pandas DataFrame using data from a 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.

pandas add row

Add Row to Pandas DataFrame

To add or insert a row to an existing DataFrame from a dictionary, you can use the append() function 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)

This example yields the below output.

pandas add row

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

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.

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

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

Add Specific Row/Index Name Using loc[]

You can use DataFrame.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

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)

FAQ an Adding

How can I add a row to the first position of a DataFrame?

You can use pd.concat([new_row,df.loc[:]]).reset_index(drop=True) to add the row to the first position of the 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.

What is the difference between append() and concat() for adding rows to a DataFrame?

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.

How can I add multiple rows to a DataFrame efficiently?

To add multiple rows efficiently, you can create a new DataFrame with the rows you want to add and then use concat it to concatenate it with the original DataFrame.

How can I add a row at a specific position in a DataFrame?

You can add a row at a specific position to the DataFrame by using df.loc[Index_label]=new_row this syntax.

Conclusion

In this article, I have explained how to add or insert a row to Pandas DataFrame using loc[], concat(), and append() functions.

Happy Learning !!

Leave a Reply

This Post Has One Comment

  1. Paulo Gurgel

    As in Pandas 2.0, the method append was deprecated and removed. One should use either concat or loc