How to insert a new row to a Pandas Dataframe?
In this post, we will learn to insert/add a new row to an existing Pandas Dataframe using pandas.DataFrame.loc, pandas.concat() and numpy.insert(). Using these methods you can add multiple rows/lists to an existing or an empty Pandas DataFrame.
Creating a Pandas Dataframe from Pandas Series objects.
First of all, we will create a Pandas Dataframe from two Pandas Series objects as under :-
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
cols = ["A", "B", "C"]
df = pd.DataFrame([list(s1), list(s2)], columns = cols)
A B C
0 1 2 3
1 4 5 6
[2 rows x 3 columns]
Now assume that we need to append the following list as a new row to the Pandas Dataframe.
new_row = [7, 8, 9]
Inserting a new row to a Pandas Dataframe using .loc
We can insert a new row as the last row to a Pandas Dataframe using pandas.DataFrame.loc as shown in the following code:-
df.loc[-1] = new_row
A B C
0 1 2 3
1 4 5 6
-1 7 8 9
[3 rows x 3 columns]
But, here the index of the last row is -1, you can reset the index of a Pandas DataFrame using df.reset_index():-
df.reset_index(drop=True)
A B C
0 1 2 3
1 4 5 6
2 7 8 9
[3 rows x 3 columns]
Inserting a new row to a Pandas Dataframe using pandas.concat()
You can also add a new row to a Pandas Dataframe using pandas.concat() by converting the row into a Dataframe and then concatenating it to the existing Dataframe as under:-
new_df = pd.DataFrame([new_row], columns=cols)
A B C
0 7 8 9
[1 row x 3 columns]
df = pd.concat([df, new_df])
A B C
0 1 2 3
1 4 5 6
0 7 8 9
[3 rows x 3 columns]
Here, the new Pandas Dataframe has preserved the index of the row, but you can reset the index while concatenating by using the argument ‘ignore_index = True’ in pandas.concat().
df = pd.concat([df, new_df], ignore_index = True)
A B C
0 1 2 3
1 4 5 6
2 7 8 9
[3 rows x 3 columns]
You can also add the new row to the top of the existing Dataframe by switching the order while using pandas.concat().
df = pd.concat([new_df, df], ignore_index = True)
A B C
0 7 8 9
1 1 2 3
2 4 5 6
[3 rows x 3 columns]
Inserting a new row to a Pandas Dataframe using numpy.insert()
You can also insert a new row to an existing Pandas Dataframe using numpy.insert(). The major advantage of using numpy.insert() to insert a new row to the Pandas Dataframe is that you can insert the new row at an arbitrary or a desired position/index in the dataframe by declaring the desired index of the row in np.insert().
import numpy as np
df = pd.DataFrame(np.insert(df.values, 1, new_row, axis=0)) # 1 is the index at which we need to insert the new row.
A B C
0 1 2 3
1 7 8 9
2 4 5 6
[3 rows x 3 columns]
Inserting a new row at a desired index to a Pandas Dataframe using pandas.concat()
Alternatively, you can also insert a new row at an arbitrary or desired index using pandas.concat() using slicing (df.iloc) as under:-
df = pd.concat
df = pd.concat([df.iloc[:1], new_df, df.iloc[1:]], ignore_index = True)
A B C
0 1 2 3
1 7 8 9
2 4 5 6
[3 rows x 3 columns]
How to add a new row to a Pandas Dataframe? - Video Tutorial
If you have any query regarding this post, feel free to comment below. Also, if you have liked this post, please share it with someone who needs it.