How can I insert a row into a Pandas DataFrame?

How can I insert a row into a Pandas DataFrame?

To insert a row into a Pandas DataFrame, first create a new row with the desired values and then use the “append()” function to add it to the DataFrame. Alternatively, you can use the “loc” function to specify the index of the new row and assign the values to it. It is important to note that the number of values in the new row must match the number of columns in the DataFrame. Additionally, make sure to reassign the DataFrame to itself in order to save the changes.

Insert a Row Into a Pandas DataFrame


You can use the following basic syntax to insert a row into a a specific location in a pandas DataFrame:

import pandas as pd
import numpy as np

#insert row with values [1, 7, 6] into existing DataFrame at index=4
pd.DataFrame(np.insert(df.values, 4, values=[1, 7, 6], axis=0))

The following example shows how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'C'],
                   'assists': [5, 7, 7, 9, 12],
                   'rebounds': [11, 8, 10, 6, 6]})

#view DataFrame
df

        team	assists	rebounds
0	A	5	11
1	A	7	8
2	B	7	10
3	B	9	6
4	C	12	6

Example 1: Insert Values Into First Row of Pandas DataFrame

We can use the following syntax to insert a row of values into the first row of a pandas DataFrame:

#insert values into first row of DataFrame
df2 = pd.DataFrame(np.insert(df.values, 0, values=['A', 3, 4], axis=0))

#define column names of DataFrame
df2.columns = df.columns#view updated DataFrame
df2

	team	assists	rebounds
0	A	3	4
1	A	5	11
2	A	7	8
3	B	7	10
4	B	9	6
5	C	12	6

Example 2: Insert Values Into Specific Row of Pandas DataFrame

We can use the following syntax to insert a row of values into a specific row of a pandas DataFrame:

#insert values into third row (index position=2) of DataFrame
df2 = pd.DataFrame(np.insert(df.values, 2, values=['A', 3, 4], axis=0))

#define column names of DataFrame
df2.columns = df.columns#view updated DataFrame
df2

        team	assists	rebounds
0	A	5	11
1	A	7	8
2	A	3	4
3	B	7	10
4	B	9	6
5	C	12	6

Example 3: Insert Values Into Last Row of Pandas DataFrame

We can use the following syntax to insert a row of values into the last row of a pandas DataFrame:

#insert values into last row of DataFrame
df2 = pd.DataFrame(np.insert(df.values, len(df.index), values=['A', 3, 4], axis=0))

#define column names of DataFrame
df2.columns = df.columns#view updated DataFrame
df2

	team	assists	rebounds
0	A	5	11
1	A	7	8
2	B	7	10
3	B	9	6
4	C	12	6
5	A	3	4

Note: You can find the complete documentation for the NumPy insert() function .

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Cite this article

stats writer (2024). How can I insert a row into a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-insert-a-row-into-a-pandas-dataframe/

stats writer. "How can I insert a row into a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-insert-a-row-into-a-pandas-dataframe/.

stats writer. "How can I insert a row into a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-insert-a-row-into-a-pandas-dataframe/.

stats writer (2024) 'How can I insert a row into a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-insert-a-row-into-a-pandas-dataframe/.

[1] stats writer, "How can I insert a row into a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I insert a row into a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top