• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:17 mins read
You are currently viewing Pandas Add or Insert Row to DataFrame

How to add or insert a row to pandas DataFrame? You can use multiple ways of Pandsa such as append(), pandas.concat(), and loc[]. In this article, I will explain how to add a row to DataFrame with several examples.

The examples include how to insert or add a Python list, dict (dictionary) as a row to pandas DataFrame, which ideally adds a new record to the DataFrame with elements specified by a list and dict.

1. Quick Examples of Adding Row to DataFrame

If you are in a hurry, below are some quick examples of how to insert/add a row to DataFrame.


# Below are the 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]

Let’s create a pandas DataFrame from Dict with a few rows and columns and execute some examples to learn how to insert rows. Our Pandas DataFrame contains column names Courses, Fee, Duration, and Discount.


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

2. Add Row to Pandas DataFrame

To add or insert a row to an existing pandas 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)

Yields 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.

3. Add or Insert List as Row to DataFrame

If you have a list and want to add/insert it to DataFrame use loc[]. For more similar examples, refer to how to append a list as a row to pandas DataFrame.


# 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)

Yields below 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

Note that when you have a default number index, it automatically increments the index and adds the row at the end of the DataFrame.

4. 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. Note that in this example, first we are creating a new DataFrame/Series and append this to another DataFrame.


# 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)

Yields below output for both examples.


# 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

5. Use concat() to Add Row at the Top of DataFrame

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

Yields below output.


# 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

6. Use DataFrame.loc[] to Add Specific Row/Index Name

By using df.loc[Index_label]=new_row you can add a list as a row to the DataFrame at a specific index. 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)

Yields below output.


# 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

7. Complete Example of Add Row to DataFrame


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)

Frequently Asked Questions of Add Row to DataFrame

How do I add a row to a Pandas DataFrame?

You can add a row to a Pandas DataFrame by using the append() method or the concat() function.

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 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 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.

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, you have learned how to add or insert a row to Pandas DataFrame using loc[], concat(), and append() methods. Using these you can add a row from the list/dictionary at any position/index.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

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