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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-68576{{"`Pandas Append Method`"}} python/with_statement -.-> lab-68576{{"`Pandas Append Method`"}} pandas/select_columns -.-> lab-68576{{"`Pandas Append Method`"}} python/variables_data_types -.-> lab-68576{{"`Pandas Append Method`"}} python/booleans -.-> lab-68576{{"`Pandas Append Method`"}} python/lists -.-> lab-68576{{"`Pandas Append Method`"}} python/tuples -.-> lab-68576{{"`Pandas Append Method`"}} python/dictionaries -.-> lab-68576{{"`Pandas Append Method`"}} python/importing_modules -.-> lab-68576{{"`Pandas Append Method`"}} python/catching_exceptions -.-> lab-68576{{"`Pandas Append Method`"}} python/data_collections -.-> lab-68576{{"`Pandas Append Method`"}} python/numerical_computing -.-> lab-68576{{"`Pandas Append Method`"}} python/data_analysis -.-> lab-68576{{"`Pandas Append Method`"}} python/build_in_functions -.-> lab-68576{{"`Pandas Append Method`"}} end

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