Javaexercise.com

Pandas DataFrame append() Method

Pandas append() method is used to append rows of other DataFrame to the end of the caller DataFrame. It returns a new DataFrame containing elements of both the DataFrames. The pandas.DataFrame.append() method is useful when we want to combine or append two DataFrames into a single DataFrame.

In case, if DataFrame that we are going to append contains different columns then these are added as new columns into the resultant DataFrame.

Let's understand it by using examples. The syntax of this method is here.

 

Syntax

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

 

Parameters

Parameter Type Default Value Required Description
other DataFrame or Series No Default Value Yes

Data that going to append. It can be DataFrame or Series/dict-like object, or list of these.

ignore_index bool False No If it is true, the resulting axis will be labeled 0 to n - 1.
verify_integrity bool False No If True, raise ValueError on creating an index with duplicates.
sort bool False No Sort columns if the columns of self and other are not aligned.

 

Return Value

It returns DataFrame that contains elements of both the DataFrames.

 

Example with Jupyter Notebook

Let's take an example to understand the use of pandas.DataFrame.append() method. Following is the code that is executed by using the Jupyter notebookIf you are familiar with Jupyter, See the screenshot below.

 

python-pandas-dataframe-append-method

 

Example: How to Append a DataFrame to another DataFrame in Pandas?

Let's append elements of a DataFrame to another DataFrame by using the append() method of DataFrame. It will append all the elements as new rows at the end of DataFrame and return a new DataFrame. See the example here. This example is similar to the above code, executed by using Jupyter notebook. If you are not familiar with Jupyter then see this code to understand.

# import pandas library
import pandas as pd
# Create DataFrame of data
df = pd.DataFrame([[12, 13, 14, 15],
    [20, 40, 50, 30],
    [-100, 40, -10, 0]],
    columns = ['A','B','C','D']
  )
# print dataframe
print(df)
# Take another DataFrame
df2 = pd.DataFrame([[200, 210, 220, 230],
    [310, 320, 330, 340]],
    columns = ['A','B','C','D']
  )
print("\nApplying append() method...\n")
# append() method
df = df.append(df2) # Append a dataframe to another dataframe
print(df)

Output:     

      A   B    C   D
0   12  13  14  15
1   20  40  50  30
2 -100  40 -10   0

Applying append() method...

      A    B     C    D
0   12   13   14   15
1   20   40   50   30
2 -100   40  -10    0
0  200  210  220  230
1  310  320  330  340

 

 

Example: Arrange DataFrame Index in Pandas

In the above example, index values of resultant DataFrame are not in order. To arrange them into an order use the ignore_index argument. Set ignore_index = True and the index will be arranged from 0 to n-1. See the example here.

# import pandas library
import pandas as pd
# Create DataFrame of data
df = pd.DataFrame([[12, 13, 14, 15],
    [20, 40, 50, 30],
    [-100, 40, -10, 0]],
    columns = ['A','B','C','D']
  )
# print dataframe
print(df)
df2 = pd.DataFrame([[200, 210, 220, 230],
    [310, 320, 330, 340]],
    columns = ['A','B','C','D']
  )
print("\nApplying append() method...\n")
# append() method
df = df.append(df2,ignore_index=True) # Append a dataframe to another dataframe
print(df)

Output:

      A   B    C   D
0   12  13  14  15
1   20  40  50  30
2 -100  40 -10   0

Applying append() method...

      A    B     C    D
0   12   13   14   15
1   20   40   50   30
2 -100   40  -10    0
3  200  210  220  230
4  310  320  330  340

 

Example: Append Series into DataFrame in Pandas

Like DataFrame, we can append Series data into DataFrame as well. See the following example here.

# import pandas library
import pandas as pd
# Create DataFrame of data
df = pd.DataFrame([[12, 13, 14, 15],
    [20, 40, 50, 30],
    [-100, 40, -10, 0]],
    columns = ['A','B','C','D']
  )
# print dataframe
print(df)
# Take a Series
s = pd.Series([200, 210, 220, 230],index=['A', 'B', 'C', 'D'])
print("\nApplying append() method...\n")
# append() method
df = df.append(s,ignore_index=True) # Append a Series to another dataframe
print(df)

Output:

      A   B   C    D
0   12  13  14  15
1   20  40  50  30
2 -100  40 -10   0

Applying append() method...

      A    B     C    D
0   12   13   14   15
1   20   40   50   30
2 -100   40  -10    0
3  200  210  220  230

 

 

Example: If Series or DataFrame has Different Columns

In case, DataFrame or Series which we want to append has different columns then Pandas will create new columns and fill them by setting NaN. In this example, we are appending Series but it can be DataFrame too.

# import pandas library
import pandas as pd
# Create DataFrame of data
df = pd.DataFrame([[12, 13, 14, 15],
    [20, 40, 50, 30],
    [-100, 40, -10, 0]],
    columns = ['A','B','C','D']
  )
# print dataframe
print(df)
s = pd.Series([200, 210, 220, 230],index=['A', 'B', 'C', 'E']) # Different Columns
print("\nApplying append() method...\n")
# append() method
df = df.append(s,ignore_index=True) # Append a Series to another dataframe
print(df)

Output:

      A   B    C   D
0   12  13  14  15
1   20  40  50  30
2 -100  40 -10   0

Applying append() method...

       A        B       C       D        E
0   12.0   13.0   14.0  15.0    NaN
1   20.0   40.0   50.0  30.0    NaN
2 -100.0   40.0  -10.0   0.0    NaN
3  200.0  210.0  220.0   NaN  230.0

 

 

Example: TypeError('Can only append a Series if ignore_index=True')

While appending Series to DataFrame, make sure to prove the ignore_index argument by setting True value, Else it will generate an error.

# import pandas library
import pandas as pd
# Create DataFrame of data
df = pd.DataFrame([[12, 13, 14, 15],
    [20, 40, 50, 30],
    [-100, 40, -10, 0]],
    columns = ['A','B','C','D']
  )
# print dataframe
print(df)
s = pd.Series([200, 210, 220, 230])
print("\nApplying append() method...\n")
# append() method
df = df.append(s) # Append a Series to another dataframe
print(df)

Output:

raise TypeError('Can only append a Series if ignore_index=True'
TypeError: Can only append a Series if ignore_index=True or if the Series has a name