Pandas Append Method

PythonPythonBeginner
Practice Now

Introduction

In this tutorial, you will learn how to use the append() method in Python Pandas. The append() method allows you to append rows from one DataFrame to another and returns a new DataFrame object. It can also add columns from the appended DataFrame if they are not already present in the calling DataFrame.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Create Two DataFrames

First, we need to create two DataFrames that we will use for the examples in this tutorial.

import pandas as pd

## Create DataFrame 1
df1 = pd.DataFrame([['Abhishek', 100, 'Science', 90],
                    ['Anurag', 101, 'Science', 85]],
                   columns=['Name', 'Roll No', 'Subject', 'Marks'])

## Create DataFrame 2
df2 = pd.DataFrame([['Chetan', 103, 'Maths', 75],
                    ['Divya', 104, 'Science', 80],
                    ['Diya', 105, 'Maths', 92]],
                   columns=['Name', 'Roll No', 'Subject', 'Marks'])

Append Rows using append() Method

You can append one DataFrame to another using the append() method. To do this, simply call the append() method on the DataFrame you want to append to, and pass the DataFrame you want to append as the parameter.

## Append DataFrame 2 to DataFrame 1
appended_df = df1.append(df2)

## Print the result
print(appended_df)

Avoid Overlapping Index Values

When you append one DataFrame to another, the index values can overlap. To avoid this, you can set the ignore_index parameter to True. This will reassign new index values to the appended DataFrame.

## Append DataFrame 2 to DataFrame 1 with ignore_index parameter set to True
appended_df = df1.append(df2, ignore_index=True)

## Print the result
print(appended_df)

Prevent Overlapping Index Values

If you want to raise an error when the index values overlap, you can set the verify_integrity parameter to True.

## Append DataFrame 2 to DataFrame 1 with verify_integrity parameter set to True
try:
    appended_df = df1.append(df2, verify_integrity=True)
except ValueError as e:
    print(e)

Append Row from Series

You can also append a single row, represented as a Series, to a DataFrame. To do this, create a Series object with the row values and index, and then call the append() method on the DataFrame with the Series as the parameter.

## Create a Series
series = pd.Series(['Chetan', 103, 'Maths', 75], index=['Name', 'Roll No', 'Subject', 'Marks'])

## Append the Series to DataFrame 1
appended_df = df1.append(series, ignore_index=True)

## Print the result
print(appended_df)

Append Row from Dictionary

You can also append a row represented as a dictionary to a DataFrame. To do this, create a dictionary with the column names as keys and the row values as values, and then call the append() method on the DataFrame with the dictionary as the parameter.

## Create a dictionary
dictionary = {'Name': 'Chetan', 'Roll No': 103, 'Subject': 'Maths', 'Marks': 75}

## Append the dictionary to DataFrame 1
appended_df = df1.append(dictionary, ignore_index=True)

## Print the result
print(appended_df)

Summary

In this tutorial, you learned how to use the append() method in Python Pandas. You can use this method to append rows from one DataFrame to another, as well as add columns from the appended DataFrame if they are not already present in the calling DataFrame. Remember to use the appropriate parameters, such as ignore_index and verify_integrity, to control the behavior of the append operation. This method allows you to easily combine and manipulate DataFrames in your data analysis tasks. Happy coding!

Other Python Tutorials you may like