How to Add or Insert Row to Pandas DataFrame?

Pandas DataFrame – Add or Insert Row

To append or add a row to DataFrame, create the new row as Series and use DataFrame.append() method.

In this tutorial, we shall learn how to append a row to an existing DataFrame, with the help of illustrative example programs.

Syntax – append()

Following is the syntax of DataFrame.appen() function.

mydataframe = mydataframe.append(new_row, ignore_index=True)

where the resulting DataFrame contains new_row added to mydataframe.

append() is immutable. It does not change the DataFrame, but returns a new DataFrame with the row appended.

Pandas Add Row to DataFrame

Example 1: Add Row to DataFrame

In this example, we will create a DataFrame and append a new row to this DataFrame. The new row is initialized as a Python Dictionary and append() function is used to append the row to the dataframe.

When you are adding a Python Dictionary to append(), make sure that you pass ignore_index=True.

The append() method returns the dataframe with the newly added row.

Python Program

import pandas as pd

data = {'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'physics': [68, 74, 77, 78],
	'chemistry': [84, 56, 73, 69],
	'algebra': [78, 88, 82, 87]}

	
#create dataframe
df_marks = pd.DataFrame(data)
print('Original DataFrame\n------------------')
print(df_marks)

new_row = {'name':'Geo', 'physics':87, 'chemistry':92, 'algebra':97}
#append row to the dataframe
df_marks = df_marks.append(new_row, ignore_index=True)

print('\n\nNew row added to DataFrame\n--------------------------')
print(df_marks)
Run this program

Output

Run the above Python program, and you shall see the original dataframe, and the dataframe appended with the new row.

Original DataFrame
------------------
   name  physics  chemistry  algebra
0  Somu       68         84       78
1  Kiku       74         56       88
2  Amol       77         73       82
3  Lini       78         69       87


New row added to DataFrame
--------------------------
   name  physics  chemistry  algebra
0  Somu       68         84       78
1  Kiku       74         56       88
2  Amol       77         73       82
3  Lini       78         69       87
4   Geo       87         92       97

Example 2: Add Row to Pandas DataFrame (ignoreIndex = False)

If you do not provide the parameter ignoreIndex=False, you will get TypeError.

In the following example, we will try to append a row to DataFrame with the parameter ignoreIndex=False.

Python Program

import pandas as pd

data = {'name': ['Amol', 'Lini'],
	'physics': [77, 78],
	'chemistry': [73, 85]}

#create dataframe
df_marks = pd.DataFrame(data)
print('Original DataFrame\n------------------')
print(df_marks)

new_row = {'name':'Geo', 'physics':87, 'chemistry':92}
#append row to the dataframe
df_marks = df_marks.append(new_row, ignore_index=False)

print('\n\nNew row added to DataFrame\n--------------------------')
print(df_marks)
Run this program

Output

Original DataFrame
------------------
   name  physics  chemistry
0  Amol       77         73
1  Lini       78         85
Traceback (most recent call last):
  File "example1.py", line 14, in <module>
    df_marks = df_marks.append(new_row, ignore_index=False)
  File "C:\Users\PythonExamples\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\frame.py", line 6658, in append
    raise TypeError('Can only append a Series if ignore_index=True'
TypeError: Can only append a Series if ignore_index=True or if the Series has a name

As the error message says, we need to either provide the parameter ignore_index=True or append the row, that is Series with a name.

We have already seen in Example 1, how to add row to the DataFrame with ignore_index=True. Now we will see how to add a row with ignore_index=False.

Python Program

import pandas as pd

data = {'name': ['Amol', 'Lini'],
	'physics': [77, 78],
	'chemistry': [73, 85]}

#create dataframe
df_marks = pd.DataFrame(data)
print('Original DataFrame\n------------------')
print(df_marks)

new_row = pd.Series(data={'name':'Geo', 'physics':87, 'chemistry':92}, name='x')
#append row to the dataframe
df_marks = df_marks.append(new_row, ignore_index=False)

print('\n\nNew row added to DataFrame\n--------------------------')
print(df_marks)
Run this program

We have named the Series as data. Therefore ignore_index=False does not return a TypeError and the row is appended to the DataFrame.

Output

Original DataFrame
------------------
   name  physics  chemistry
0  Amol       77         73
1  Lini       78         85


New row added to DataFrame
--------------------------
   name  physics  chemistry
0  Amol       77         73
1  Lini       78         85
x   Geo       87         92

Summary

In this Pandas Tutorial, we have used append() function to add a row to Pandas DataFrame.