Pandas DataFrame append() function in Python

In this tutorial, you will learn about the dataframe.append() function of the pandas library in Python.

dataframe.append() function is used to append rows of one dataframe at the end of another dataframe. If the columns are not present in the dataframe to which another dataframe is being appended, then those columns are appended as new columns and stored with NaN value.

Syntax : DataFrame.append(self, other, ignore_index=False, verify_integrity=False, sort=None)

other: Dataframe
ignore_index: default False; If True, don't use index labels
verify_integrity: default False; If true, raise ValueError for duplicates.
sort: default None; Sort columns if columns are not aligned.
returns: DataFrame

Let’s first create two dataframes.

Dataframe 1 :

import pandas as pd
data1 = {
        'id': ['1', '2', '3'],
        
        'Name': ['Alex', 'Ben', 'Chetan']}
d1 = pd.DataFrame(data1, columns = ['id','Name'])
print(d1)
output :

  id    Name
0  1    Alex
1  2     Ben
2  3    Mark

Dataframe 2:

data2 = {
        'id': ['1', '2', '3', '4'],
        
        'Name': ['Felix', 'Chetan', 'Thomas', 'Deepak']}
d2 = pd.DataFrame(data2, columns = ['id','Name'])
print(d2)
Output :
  id    Name
0  1   Felix
1  2  Chetan
2  3  Thomas
3  4  Deepak

Now we have created 2 dataframes, lets append dataframe 2 to dataframe 1 using append() function.

import pandas as pd

data1 = {
        'id': ['1', '2', '3'],
        
        'Name': ['Alex', 'Ben', 'Mark']}
d1 = pd.DataFrame(data1, columns = ['id','Name'])

data2 = {
        'id': ['1', '2', '3', '4'],
        
        'Name': ['Felix', 'Chetan', 'Thomas', 'Deepak']}
d2 = pd.DataFrame(data2, columns = ['id','Name'])

print("\nUsing append() function\n")
print(d1.append(d2))
Output :
Using append() function

  id    Name
0  1    Alex
1  2     Ben
2  3    Mark
0  1   Felix
1  2  Chetan
2  3  Thomas
3  4  Deepak

We can notice that the index values of the second dataframe are maintained after being appended. We can prevent this by setting ignore_index to True

import pandas as pd
data1 = {
        'id': ['1', '2', '3'],
        
        'Name': ['Alex', 'Ben', 'Mark']}
d1 = pd.DataFrame(data1, columns = ['id','Name'])
data2 = {
        'id': ['1', '2', '3', '4'],
        
        'Name': ['Felix', 'Chetan', 'Thomas', 'Deepak']}
d2 = pd.DataFrame(data2, columns = ['id','Name'])

print("\nUsing append() function with ignore_index set True\n")
print(d1.append(d2,ignore_index = True))
Output : 
Using append() function with ignore_index set True

  id    Name
0  1    Alex
1  2     Ben
2  3    Mark
3  1   Felix
4  2  Chetan
5  3  Thomas
6  4  Deepak

Now you might be wondering what if the dataframe being appended has more columns.
In this case, the dataframe which has nonexistent value is filled with NaN values.

import pandas as pd

data1 = {
        'id': ['1', '2', '3'],
        
        'Name': ['Alex', 'Ben', 'Mark']}
d1 = pd.DataFrame(data1, columns = ['id','Name'])

data2 = {
        'id': ['1', '2', '3', '4'],
        
        'Name': ['Felix', 'Chetan', 'Thomas', 'Deepak'],
        
        'Country': ['Europe','India','United States','India']}
d2 = pd.DataFrame(data2, columns = ['id','Name','Country'])

print()

print(d1.append(d2,ignore_index = True))
Output : 
         Country    Name id
0            NaN    Alex  1
1            NaN     Ben  2
2            NaN    Mark  3
3         Europe   Felix  1
4          India  Chetan  2
5  United States  Thomas  3
6          India  Deepak  4

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *