We use pandas dataframes to manipulate tabular data in Python. In this article, we will discuss different ways to insert a row into a pandas dataframe.
Insert Row in A Pandas DataFrame
To insert a row in a pandas dataframe, we can use a list or a Python dictionary. Let us discuss both approaches.
Insert a Dictionary to a DataFrame in Python
We will use the pandas append method to insert a dictionary as a row in the pandas dataframe. The append()
method, when invoked on a pandas dataframe, takes a dictionary containing the row data as its input argument. After execution, it inserts the row at the bottom of the dataframe. You can observe this in the following example.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRow={"Roll":11,"Maths":99, "Physics":75, "Chemistry": 85}
print("The new row is:")
print(newRow)
output_df=df.append(newRow, ignore_index=True)
print("The output dataframe is:")
print(output_df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new row is:
{'Roll': 11, 'Maths': 99, 'Physics': 75, 'Chemistry': 85}
The output dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
6 11 99 75 85
In the above example, we first created a list of dictionaries. Then, we converted the list of dictionaries to dataframe using the DataFrame()
function. Finally, we used the append()
method to add a row at the bottom of the dataframe. Here, we need to set the ignore_index
parameter to True
in the append()
method. Otherwise, the program will run into an error.
The append()
method will be deprecated in future versions of pandas. Therefore, you can use the concat()
method to insert a row into a dataframe. For this, we will use the following steps.
- First, we will put the dictionary containing the row data into a list.
- Next, we will use the
DataFrame()
function to create a pandas dataframe using the list containing the row data. - After creating the dataframe, we will use the
concat()
method to insert the new row into the existing dataframe. Theconcat()
function takes a list of all the dataframes and concatenates them.
After execution of the concat()
function, we will get a new dataframe with the new row inserted at its bottom. You can observe this in the following example.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRow={"Roll":11,"Maths":99, "Physics":75, "Chemistry": 85}
print("The new row is:")
print(newRow)
row_df=pd.DataFrame([newRow])
output_df=pd.concat([df,row_df], ignore_index=True)
print("The output dataframe is:")
print(output_df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new row is:
{'Roll': 11, 'Maths': 99, 'Physics': 75, 'Chemistry': 85}
The output dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
6 11 99 75 85
In this example, we used the concat()
function instead of the append()
method to insert a row into the dataframe. Here, you can observe that we have created a new dataframe containing the new row. Then we used the concat()
method to concatenate the dataframes. Here, the order of the dataframes in the concat()
function determines where the new row will be added in the output dataframe.
Pandas Insert a List into a Row in a DataFrame
To insert a list into a pandas dataframe as its row, we will use the len()
function to find the number of rows in the existing dataframe. The len()
function takes the dataframe as its input argument and returns the total number of rows.
Next, we will use the number of rows and the loc
attribute of the dataframe to insert the list in the dataframe as shown in the following example.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRow=[11,99,75,85]
print("The new row is:")
print(newRow)
num_rows=len(df)
df.loc[num_rows]=newRow
print("The output dataframe is:")
print(df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new row is:
[11, 99, 75, 85]
The output dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
6 11 99 75 85
In this example, we first created a dataframe and calculated the number of rows in it. Next, we used the loc
attribute to insert a new row into the pandas dataframe.
In the above code, we cannot use the iloc
attribute to assign the new row. The iloc
attribute cannot change the dataframe. Hence, you will get a python indexerror exception saying that the iloc
attribute cannot enlarge its target object. You can observe this in the following example.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRow=[11,99,75,85]
print("The new row is:")
print(newRow)
num_rows=len(df)
df.iloc[num_rows]=newRow
print("The output dataframe is:")
print(df)
Output:
IndexError: iloc cannot enlarge its target object
Insert a Row at the Start of a Pandas DataFrame
Instead of inserting a new row at the end of a dataframe, we can also insert it at the start of the dataframe.
To insert a dictionary at the beginning of a dataframe as a row, we can use the concat()
method. Here, we will first create a new dataframe from the dictionary. After that, we can use the concat()
function to insert the new dataframe at the top of the existing dataframe as shown below.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRow={"Roll":11,"Maths":99, "Physics":75, "Chemistry": 85}
print("The new row is:")
print(newRow)
row_df=pd.DataFrame([newRow])
output_df=pd.concat([row_df,df], ignore_index=True)
print("The output dataframe is:")
print(output_df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new row is:
{'Roll': 11, 'Maths': 99, 'Physics': 75, 'Chemistry': 85}
The output dataframe is:
Roll Maths Physics Chemistry
0 11 99 75 85
1 1 100 80 90
2 2 80 100 90
3 3 90 80 70
4 4 100 100 90
5 5 90 90 80
6 6 80 70 70
In the above example, we have put the new dataframe containing the new row ahead of the existing dataframe in the concat()
function. Hence, the new row is inserted at the top of the input dataframe.
Pandas Insert a Row at a Specific Position in a DataFrame
To insert a row at a specific position in a dataframe, we will use the following steps.
- First, we will split the input dataframe at the given position using the
iloc
attribute. For instance, if we have to insert a new row at the Nth position, we will split the rows at position 0 to N-1 in a single dataframe and the rows at position N till the end into another dataframe. - Next, we will create a new dataframe containing the new row using the
DataFrame()
function. - After this, we will combine the new dataframe and the split dataframes using the
concat()
function. After execution of theconcat()
function, we will get the new dataframe having the new row at the given position.
You can observe this in the following example.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRow={"Roll":11,"Maths":99, "Physics":75, "Chemistry": 85}
print("The new row is:")
print(newRow)
row_df=pd.DataFrame([newRow])
df_upper=df.iloc[:2]
df_lower=df.iloc[2:]
output_df=pd.concat([df_upper,row_df,df_lower], ignore_index=True)
print("The output dataframe is:")
print(output_df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new row is:
{'Roll': 11, 'Maths': 99, 'Physics': 75, 'Chemistry': 85}
The output dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 11 99 75 85
3 3 90 80 70
4 4 100 100 90
5 5 90 90 80
6 6 80 70 70
In this example, we have inserted a new row at the third position (index 2) of the original dataframe. So, we have used the iloc
attribute to split the input dataframe at index 2 i.e position 3. Next, we created a new dataframe containing the new row. Finally, we used the concat()
method to sandwich the dataframe containing the new row between the parts of the original dataframe.
Insert Multiple Rows in a Pandas DataFrame
To insert multiple rows in a dataframe, you can use a list of dictionaries and convert them into a dataframe. Then, you can insert the new dataframe into the existing dataframe using the contact()
function. The process is exactly the same as inserting a single row. The only difference is that the new dataframe that we insert into the existing dataframe will have multiple rows.
To insert multiple rows at the beginning of an existing dataframe, you can use the new dataframe with the concat()
function as shown below.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRows=[{"Roll":11,"Maths":99, "Physics":75, "Chemistry": 85},
{"Roll":12,"Maths":89, "Physics":85, "Chemistry": 88}]
print("The new rows are:")
print(newRows)
row_df=pd.DataFrame(newRows)
output_df=pd.concat([row_df,df], ignore_index=True)
print("The output dataframe is:")
print(output_df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new rows are:
[{'Roll': 11, 'Maths': 99, 'Physics': 75, 'Chemistry': 85}, {'Roll': 12, 'Maths': 89, 'Physics': 85, 'Chemistry': 88}]
The output dataframe is:
Roll Maths Physics Chemistry
0 11 99 75 85
1 12 89 85 88
2 1 100 80 90
3 2 80 100 90
4 3 90 80 70
5 4 100 100 90
6 5 90 90 80
7 6 80 70 70
You can insert multiple rows at the end of a dataframe as shown below.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRows=[{"Roll":11,"Maths":99, "Physics":75, "Chemistry": 85},
{"Roll":12,"Maths":89, "Physics":85, "Chemistry": 88}]
print("The new rows are:")
print(newRows)
row_df=pd.DataFrame(newRows)
output_df=pd.concat([df,row_df], ignore_index=True)
print("The output dataframe is:")
print(output_df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new rows are:
[{'Roll': 11, 'Maths': 99, 'Physics': 75, 'Chemistry': 85}, {'Roll': 12, 'Maths': 89, 'Physics': 85, 'Chemistry': 88}]
The output dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
6 11 99 75 85
7 12 89 85 88
To insert multiple rows at a specific position in a dataframe, you can create a dataframe of new rows. Then, you can insert the newly created dataframe at the desired position of the original dataframe using the contact()
function as shown below.
import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
{"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
{"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
{"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
{"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
{"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The input dataframe is:")
print(df)
newRows=[{"Roll":11,"Maths":99, "Physics":75, "Chemistry": 85},
{"Roll":12,"Maths":89, "Physics":85, "Chemistry": 88}]
print("The new rows are:")
print(newRows)
row_df=pd.DataFrame(newRows)
df_upper=df.iloc[:2]
df_lower=df.iloc[2:]
output_df=pd.concat([df_upper,row_df,df_lower], ignore_index=True)
print("The output dataframe is:")
print(output_df)
Output:
The input dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 3 90 80 70
3 4 100 100 90
4 5 90 90 80
5 6 80 70 70
The new rows are:
[{'Roll': 11, 'Maths': 99, 'Physics': 75, 'Chemistry': 85}, {'Roll': 12, 'Maths': 89, 'Physics': 85, 'Chemistry': 88}]
The output dataframe is:
Roll Maths Physics Chemistry
0 1 100 80 90
1 2 80 100 90
2 11 99 75 85
3 12 89 85 88
4 3 90 80 70
5 4 100 100 90
6 5 90 90 80
7 6 80 70 70
Here, we have inserted new rows after index 2 of the existing dataframe. For this, we followed the same approach as we did while inserting a single row into the dataframe at the same index.
Conclusion
In this article, we discussed different ways to insert a row into a pandas dataframe. To learn more about Python programming, you can read this article on pyspark vs pandas. You might also like this article on how to convert a string into a dataframe in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.