Skip to content

How to Add / Insert a Row into a Pandas DataFrame

Add insert rows to pandas dataframe cover image

In this tutorial, you’ll learn how to add (or insert) a row into a Pandas DataFrame. You’ll learn how to add a single row, multiple rows, and at specific positions. You’ll also learn how to add a row using a list, a Series, and a dictionary.

By the end of this tutorial, you’ll have learned:

  • Different ways to add a single and multiple rows to a Pandas DataFrame
  • How to insert a row at particular positions, such as the top or bottom, of a Pandas DataFrame
  • How to add rows using lists, Pandas Series, and dictionaries

Loading a Sample Pandas DataFrame

To follow along with this tutorial line-by-line, you can copy the code below into your favourite code editor. If you have your own data to follow along with, feel free to do so (though your results will, of course, vary):

# Loading a Sample Pandas DataFrame
import pandas as pd

df = pd.DataFrame.from_dict({
    'Name': ['Nik', 'Kate', 'Evan', 'Kyra'],
    'Age': [31, 30, 40, 33],
    'Location': ['Toronto', 'London', 'Kingston', 'Hamilton']
})

print(df)

# Returns:
#    Name  Age  Location
# 0   Nik   31   Toronto
# 1  Kate   30    London
# 2  Evan   40  Kingston
# 3  Kyra   33  Hamilton

We have four records and three different columns, covering a person’s Name, Age, and Location.

Add a Row to a Pandas DataFrame

The easiest way to add or insert a new row into a Pandas DataFrame is to use the Pandas .append() method. The .append() method is a helper method, for the Pandas concat() function. To learn more about how these functions work, check out my in-depth article here. In this section, you’ll learn three different ways to add a single row to a Pandas DataFrame.

Add a Row to a Pandas DataFrame Using a Dictionary

Let’s say that we wanted to add a new row containing the following data: {'Name':'Jane', 'Age':25, 'Location':'Madrid'}.

We could simply write:

df = df.append({'Name':'Jane', 'Age':25, 'Location':'Madrid'}, ignore_index=True)

print(df)

# Returns:
#    Name  Age  Location
# 0   Nik   31   Toronto
# 1  Kate   30    London
# 2  Evan   40  Kingston
# 3  Kyra   33  Hamilton
# 4  Jane   25    Madrid

In the example above, we were able to add a new row to a DataFrame using a dictionary. Because we passed in a dictionary, we needed to pass in the ignore_index=True argument.

Add a Row to a Pandas DataFrame Using a List

To add a list to a Pandas DataFrame works a bit differently since we can’t simply use the .append() function. In order to do this, we need to use the loc accessor. The label that we use for our loc accessor will be the length of the DataFrame. This will create a new row as shown below:

df.loc[len(df)] = ['Jane', 25, 'Madrid']
print(df)

# Returns:
#    Name  Age  Location
# 0   Nik   31   Toronto
# 1  Kate   30    London
# 2  Evan   40  Kingston
# 3  Kyra   33  Hamilton
# 4  Jane   25    Madrid

As a fun aside: using iloc is more challenging since it requires that the index position already exist – meaning we would need to either add an empty row first or overwrite data.

Add a Row to a Pandas DataFrame Using a Series

Now let’s try to add the same row as shown above using a Pandas Series, that we can create using a Python list. We simply pass a list into the Series() function to convert the list to a Series. Let’s see how this works:

# Add a Row as a Series
df = df.append(pd.Series(['Jane', 25, 'Madrid']), ignore_index=True)
print(df)

# Returns:
#    Name  Age  Location
# 0   Nik   31   Toronto
# 1  Kate   30    London
# 2  Evan   40  Kingston
# 3  Kyra   33  Hamilton
# 4  Jane   25    Madrid

Insert a Row to a Pandas DataFrame at the Top

Adding a row to the top of a Pandas DataFrame is quite simple: we simply reverse the options you learned about above. By this, I mean to say we append the larger DataFrame to the new row.

However, we must first create a DataFrame. We can do this using the pd.DataFrame() class. Let’s take a look:

df = pd.DataFrame([['Jane', 25, 'Madrid']], columns=df.columns).append(df)
print(df)

# Returns:
#    Name  Age  Location
# 0  Jane   25    Madrid
# 0   Nik   31   Toronto
# 1  Kate   30    London
# 2  Evan   40  Kingston
# 3  Kyra   33  Hamilton

Insert a Row to a Pandas DataFrame at a Specific Index

Adding a row at a specific index is a bit different. As shown in the example of using lists, we need to use the loc accessor. However, inserting a row at a given index will only overwrite this. What we can do instead is pass in a value close to where we want to insert the new row.

For example, if we have current indices from 0-3 and we want to insert a new row at index 2, we can simply assign it using index 1.5. Let’s see how this works:

# Inserting a Row at a Specific Index
df.loc[1.5] = ['Jane', 25, 'Madrid']
df = df.sort_index().reset_index(drop=True)

print(df)

# Returns:
#    Name  Age  Location
# 0   Nik   31   Toronto
# 1  Kate   30    London
# 2  Jane   25    Madrid
# 3  Evan   40  Kingston
# 4  Kyra   33  Hamilton

This, of course, makes a few assumptions:

  1. Your index starts at 0. Adjust your loc index accordingly, if not.
  2. That your index can be mutated in this way. If your index is more meaningful, this may not be the case.

Insert Multiple Rows in a Pandas DataFrame

Adding multiple rows to a Pandas DataFrame is the same process as adding a single row. However, it can actually be much faster, since we can simply pass in all the items at once. For example, if we add items using a dictionary, then we can simply add them as a list of dictionaries.

Let’s take a look at an example:

# Adding multiple rows to a Pandas DataFrame
new_rows = [{'Name': 'Jane', 'Age': 25, 'Location': 'Madrid'}, {'Name': 'Mel', 'Age': 23, 'Location':'New York'}]

df = df.append(new_rows, ignore_index=True)

print(df)

# Returns:
#    Name  Age  Location
# 0   Nik   31   Toronto
# 1  Kate   30    London
# 2  Evan   40  Kingston
# 3  Kyra   33  Hamilton
# 4  Jane   25    Madrid
# 5   Mel   23  New York

Conclusion

In this tutorial, you learned how to add and insert rows into a Pandas DataFrame. You learned a number of different methods to do this, including using dictionaries, lists, and Pandas Series. You also learned how to insert new rows at the top, bottom, and at a particular index. Finally, you also learned how to add multiple rows to a Pandas DataFrame at the same time.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.