Signup/Sign In
LAST UPDATED: MARCH 12, 2021

Pandas append() Method

In this tutorial, we will learn the python pandas DataFrame.append() method. This method appends rows of others to the end of the caller and returns a new object, and Columns in others that are not in the caller are added as new columns.

The below is the syntax of the DataFrame.append() method. In this tutorial, we will add DataFrame to DataFrame, Series to DataFrame, and dictionary to DataFrame.

Syntax

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)

Parameters

other: It indicates the data to append. It includes the DataFrame or Series/dict-like object, or list of these.

ignore_index: It represents the bool(True or False), the default value is False. If it is True, the resulting axis will be labeled from 0, 1, …, n - 1.

verify_integrity: It represents the bool(True or False), the default value is False. If it is True, raise ValueError on creating an index with duplicates.

sort: It represents the bool(True or False), the default value is False. It sorts the columns if the columns of self and other are not aligned.

Example: Create two DataFrames

Create two DataFrame and print the output. In this tutorial, we will use these two DataFrames.

import pandas as pd
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
df2 = pd.DataFrame([['Chetan',103,'Maths',75], ['Divya',104,'Science',80], ['Diya',105,'Maths',92]], columns=['Name', 'Roll No','Subject', 'Marks'])
print("-------DataFrame 1---------")
print(df1)
print("-------DataFrame 2---------")
print(df2)

Once we run the program we will get the following output.


-------DataFrame 1---------
Name Roll No Subject Marks
0 Abhishek 100 Science 90
1 Anurag 101 Science 85
-------DataFrame 2---------
Name Roll No Subject Marks
0 Chetan 103 Maths 75
1 Divya 104 Science 80
2 Diya 105 Maths 92

Example 1: Appending multiple rows using DataFrame.append() Method

We can append a DataFrame to a DataFrame using DataFrame.append() method and the below example shows the same.

import pandas as pd
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
df2 = pd.DataFrame([['Chetan',103,'Maths',75], ['Divya',104,'Science',80], ['Diya',105,'Maths',92]], columns=['Name', 'Roll No','Subject', 'Marks'])
print(df1.append(df2))

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 Abhishek 100 Science 90
1 Anurag 101 Science 85
0 Chetan 103 Maths 75
1 Divya 104 Science 80
2 Diya 105 Maths 92

Example 2: Appending multiple rows to the DataFrame using the DataFrame.append() Method

When we append DataFrame to a DataFrame, both DataFrames index values overlap. To avoid this, we can assign ignore_index=True.

import pandas as pd
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
df2 = pd.DataFrame([['Chetan',103,'Maths',75], ['Divya',104,'Science',80], ['Diya',105,'Maths',92]], columns=['Name', 'Roll No','Subject', 'Marks'])
print(df1.append(df2,ignore_index=True))

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 Abhishek 100 Science 90
1 Anurag 101 Science 85
2 Chetan 103 Maths 75
3 Divya 104 Science 80
4 Diya 105 Maths 92

Example 3: Appending multiple rows to the DataFrame using the DataFrame.append() Method

In the last example, we learned how can we append DataFrame to a DataFrame without overlapping the index values. In this example, if we want to represent an overlap of index values we can assign verify_integrity=True in the syntax. This will throw exceptions when the index value overlaps.

import pandas as pd
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
df2 = pd.DataFrame([['Chetan',103,'Maths',75], ['Divya',104,'Science',80], ['Diya',105,'Maths',92]], columns=['Name', 'Roll No','Subject', 'Marks'])
print(df1.append(df2,verify_integrity=True)) 

Once we run the program we will get the following output.


ValueError: Indexes have overlapping values: Int64Index([0, 1], dtype='int64')

Example 4: Appending single row (Series) to DataFrame using DataFrame.append() Method

We can append a Series to a DataFrame. The below example shows the same.

import pandas as pd
series=pd.Series(['Chetan',103,'Maths',75],index=['Name', 'Roll No','Subject', 'Marks'])
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print(series)
print("------DataFrame after appending series------")
print(df1.append(series,ignore_index=True))

Once we run the program we will get the following output.


Name Chetan
Roll No 103
Subject Maths
Marks 75
dtype: object
------DataFrame after appending series------
Name Roll No Subject Marks
0 Abhishek 100 Science 90
1 Anurag 101 Science 85
2 Chetan 103 Maths 75

Example 5: Appending dictionary to DataFrame using DataFrame.append() Method

The below example shows how to append a dictionary to the DataFrame.

import pandas as pd
dictionary={'Name':'Chetan','Roll No':103,'Subject':'Maths','Marks':75}
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print(df1.append(dictionary,ignore_index=True))

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 Abhishek 100 Science 90
1 Anurag 101 Science 85
2 Chetan 103 Maths 75

Example 6: Appending list of dictionaries to DataFrame using DataFrame.append() Method

The below example shows how to append a list of dictionaries to the DataFrame.

import pandas as pd
dictionary=[{'Name':'Chetan','Roll No':103,'Subject':'Maths','Marks':75},{'Name':'Divya','Roll No':104,'Subject':'Science','Marks':80}]
df1 = pd.DataFrame([['Abhishek',100,'Science',90], ['Anurag',101,'Science',85]], columns=['Name', 'Roll No', 'Subject', 'Marks'])
print(df1.append(dictionary,ignore_index=True))

Once we run the program we will get the following output.


Name Roll No Subject Marks
0 Abhishek 100 Science 90
1 Anurag 101 Science 85
2 Chetan 103 Maths 75
3 Divya 104 Science 80

Conclusion

In this tutorial, we learned the python pandas DataFrame.append() method. We understood the syntax, parameters of the DataFrame.append() method and solved examples by applying this method on the DataFrame.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.