Pandas DataFrame – Add Row

You can add one or more rows to Pandas DataFrame using pandas.DataFrame.append() method.

In this tutorial, we will learn how to add one or more rows/records to a Pandas DataFrame, with the help of examples.

Syntax

The syntax of DataFrame.append() method is

df = df.append(new_row, ignore_index=True)
Try Online

where df is the DataFrame and new_row is the row appended to DataFrame.

append() returns a new DataFrame with the new row added to original dataframe. Original DataFrame is not modified by append() method.

ADVERTISEMENT

Add Row (Python Dictionary) to Pandas DataFrame

In the following Python example, we will initialize a DataFrame and then add a Python Dictionary as row to the DataFrame, using append() method.

The python dictionary should contain the column names as keys and corresponding values as dictionary values.

Python Example

import pandas as pd

#initialize a dataframe
df = pd.DataFrame({
	'a':[14, 52, 46],
	'b':[32, 85, 64],
	'c':[88, 47, 36]})
	
#new row as dictionary
row1 = {'a':11, 'b':22, 'c':33}

#append row to dataframe
df = df.append(row1, ignore_index=True)

#print the dataframe
print(df)
Try Online

Output

a   b   c
0  14  32  88
1  52  85  47
2  46  64  36
3  11  22  33

append() method has created a new DataFrame and added the new row to this DataFrame.

Add Row (Pandas Series) to Pandas DataFrame

In the following Python example, we will initialize a DataFrame and then add a Pandas Series as row to the DataFrame, using append() method.

Python Example

import pandas as pd

#initialize a dataframe
df = pd.DataFrame({
	'a':[14, 52, 46],
	'b':[32, 85, 64],
	'c':[88, 47, 36]})
	
#new row as Pandas Series
row1 = pd.Series(data={'a':11, 'b':22, 'c':33}, name=len(df))

#append row to dataframe
df = df.append(row1)

#print the dataframe
print(df)
Try Online

Output

a   b   c
0  14  32  88
1  52  85  47
2  46  64  36
3  11  22  33

While initializing the Series, we assigned len(df) to name. name is translated to index in DataFrame. You can assign any value of your choice to the name based on requirement.

Conclusion

In this Pandas Tutorial, we learned how to add or append a row to Pandas DataFrame using append() method.