Python Pandas

Pandas Insert Row

For data manipulation and analysis, Pandas is a software library used in Python. A panda is a one-dimensional series that can hold data including integers, strings, and floats. It uses the Series() function for creating a series. Sometimes while handling data, we need to add new rows in Pandas DataFrame. Python provides various functions to insert/add a row in Pandas DataFrame. This article will cover how to add or insert rows into Pandas using the below content:

How to Insert/Add Row in Pandas DataFrame?

To insert or add a row in Pandas DataFrame the following steps are used in Python:

Loading a Sample Pandas DataFrame

The first thing we are going to do is import the Pandas library as pd. After that, we will add Name, Age, and Location using the syntax mentioned below.

#importing pandas as pd in Sample Pandas DataFrame

df = pd.DataFrame.from_dict({
    'Name': ['joy', 'Kate'],
    'Age': [20, 30],
    'Location': ['Toronto', 'London']
})
print(df)

 

Output

You can see the output. We have two records with three different columns including information about the person’s name, age, and location as we have mentioned in the code.

Inserting a Row into Pandas

We can use the .append() method and loc operator for inserting rows into the Pandas DataFrame. In this section, we are going to cover two different methods for inserting rows into a Pandas DataFrame.

Method 1: Adding a Row Using append() Method into Pandas

The append() method is used to append or add a row into Pandas DataFrame. Let’s add a new row, with the person’s name, age, and location, for example.

{'Name':'Kate','Location':'Paris'}.

 

Using the .append() method can simply write:

import pandas as pd
df = pd.DataFrame.from_dict({
    'Name': ['Nik', 'Evan', 'Kyra'],
    'Location': ['Toronto', 'Kingston', 'Hamilton']
})
df = df.append({'Name':'kate','Location':'Paris'}, ignore_index=True)
print(df)

 

Output

You have learned now how to insert a new row into Pandas DataFrame using the .append() method. We have added a new row containing information {‘Name’:’kate’,’Location’:’Paris’}.

Method 2: Inserting a Row into Pandas Using loc Operator

In this method we are going to add a row to a list using .loc, this will create a new row in Pandas DataFrame. Let’s say we want to enter a new row containing information:

{'Name':'Jane','Location':'Madrid'}

 

So, we can simply write:

import pandas as pd
df = pd.DataFrame.from_dict({
    'Name': ['Nik', 'Kate', 'Evan', 'Kyra'],
    'Location': ['Toronto', 'London', 'Kingston', 'Hamilton']
})

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

 

Output

In the above-mentioned result, you can see we have successfully added a new row to the list using the .loc operator in Python’s DataFrame.

Conclusion

In this article, you have gained complete knowledge of inserting rows into Pandas DataFrame. By following the above-illustrated methods, you can add a new row or multiple rows to the list. You can also add multiple rows at a time by just adding rows with the current row and so on.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.