The Wayback Machine - https://web.archive.org/web/20240530234513/https://www.geeksforgeeks.org/python-pandas-dataframe-append/
Open In App

Python – Pandas dataframe.append()

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.append() function is used to append rows of other data frames to the end of the given data frame, returning a new data frame object. Columns not in the original data frames are added as new columns and the new cells are populated with NaN value.

Pandas dataframe.append() Syntax

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

Parameters:

  • other: DataFrame or Series/dict-like object, or list of these 
  • ignore_index: If True, do not use the index labels. 
  • verify_integrity: If True, raise ValueError on creating an index with duplicates. 
  • sortPandas: Sort columns if the columns of self and other are not aligned. The default sorting is deprecated and will change to not-sorting in a future version of pandas. Explicitly pass sort=True to silence the warning and sort. Explicitly pass sort=False to silence the warning and not sort. 

Return Type: appended : DataFrame

NOTE: As of Pandas version 2.0, the .append() method is no longer in use. It is important to keep this in mind while working with Pandas. More efficient alternatives for concatenating DataFrames are the .concat() function from the pandas.DataFrame module.

dataframe.append() in Pandas Examples

Create and Append a Dataframe into another Dataframe

In this example, we are creating two data frames and append the second to the first one. In this part we are just creating a dataframe and then printing the dataframes.

Python3
# Importing pandas as pd
import pandas as pd

# Creating the first Dataframe using dictionary
df1 = df = pd.DataFrame({"a":[1, 2, 3, 4],
                        "b":[5, 6, 7, 8]})

# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],
                    "b":[5, 6, 7]})

# Print df1
print("Printing df1")
print(df1, "\n")

# Print df2
print("Printing df2")
df2

Output

Printing df1
a b
0 1 5
1 2 6
2 3 7
3 4 8
Printing df2
a b
0 1 5
1 2 6
2 3 7

In this part, we are appending df2 at the end of df1. 

Python3
# to append df2 at the end of df1 dataframe
df1.append(df2)

Output

     a    b
0 1 5
1 2 6
2 3 7
3 4 8
0 1 5
1 2 6
2 3 7

Pandas Append Two DataFrames Ignore Index

Notice the index value of the second data frame is maintained in the appended data frame. If we do not want it to happen then we can set ignore_index=True. 

Python3
# A continuous index value will be maintained
# across the rows in the new appended data frame.
df1.append(df2, ignore_index=True)

Output

   a  b
0 1 5
1 2 6
2 3 7
3 4 8
4 1 5
5 2 6
6 3 7

Append Dict as Row to DataFrame

In this example, we are appending dictionary as row to dataframe.

Python3
import pandas as pd

# Creating the first Dataframe using dictionary
df1 = df = pd.DataFrame({"a": [1, 2, 3, 4],
                         "b": [5, 6, 7, 8]})

# Append Dict as row to DataFrame
new_row = {"a": 10, "b": 10}
df2 = df.append(new_row, ignore_index=True)

print(df2)

Output

    a   b 
0 1 5
1 2 6
2 3 7
3 4 8
4 10 10

Append Dataframe of Different Shapes

In this example, we are appending dataframe of different shapes. For unequal no. of columns in the data frame, a non-existent value in one of the dataframe will be filled with NaN values. 

Python3
# Importing pandas as pd
import pandas as pd

# Creating the first Dataframe using dictionary
df1 = pd.DataFrame({"a":[1, 2, 3, 4],
                    "b":[5, 6, 7, 8]})

# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],
                    "b":[5, 6, 7],
                    "c":[1, 5, 4]})

# for appending df2 at the end of df1
df1 = df1.append(df2, ignore_index = True)
df1

Output

   a  b    c
0 1 5 NaN
1 2 6 NaN
2 3 7 NaN
3 4 8 NaN
4 1 5 1.0
5 2 6 5.0
6 3 7 4.0

Notice, that the new cells are populated with NaN values.

Note: pandas.DataFrame.append is deprecated since version 1.4.0 . Rather use concat() .



Previous Article
Next Article

Similar Reads

How to append a list as a row to a Pandas DataFrame in Python?
Prerequisite: Pandas DataFrame In this article, We are going to see how to append a list as a row to a pandas dataframe in Python. It can be done in three ways: Using loc[]Using iloc[]Using append()Append list using loc[] methods Pandas DataFrame.loc attribute access a group of rows and columns by label(s) or a boolean array in the given DataFrame.
3 min read
Append list of dictionary and series to a existing Pandas DataFrame in Python
In this article, we will discuss how values from a list of dictionaries or Pandas Series can be appended to an already existing pandas dataframe. For this purpose append() function of pandas, the module is sufficient. Syntax: DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None) Parameters :other : DataFrame or Series/dict-
2 min read
Append data to an empty Pandas DataFrame
Let us see how to append data to an empty Pandas DataFrame. Creating the Data Frame and assigning the columns to it # importing the module import pandas as pd # creating the DataFrame of int and float a = [[1, 1.2], [2, 1.4], [3, 1.5], [4, 1.8]] t = pd.DataFrame(a, columns =["A", "B"]) # displaying the DataFrame print(t) print(t
2 min read
Pandas Append Rows & Columns to Empty DataFrame
Let’s discuss how to create an empty DataFrame and append rows & columns to it in Pandas and Python. There are multiple ways in which we can do this task. Here we will cover the following section: Creating an empty Dataframe in PandasAppend row to Dataframe in PandasAppend row to Dataframe in PandasCreating empty DataframeCreating an Empty Data
3 min read
How to Append Pandas DataFrame to Existing CSV File?
In this discussion, we'll explore the process of appending a Pandas DataFrame to an existing CSV file using Python. Add Pandas DataFrame to an Existing CSV File. To achieve this, we can utilize the to_csv() function in Pandas with the 'a' parameter to write the DataFrame to the CSV file in append mode. Pandas DataFrame to_csv() Syntax Syntax : df.t
3 min read
Python | Pandas DataFrame.fillna() to replace Null values in dataframe
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Just like the pandas dropna() method manages and rem
3 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()?
We might sometimes need a tidy/long-form of data for data analysis. So, in python's library Pandas there are a few ways to reshape a dataframe which is in wide form into a dataframe in long/tidy form. Here, we will discuss converting data from a wide form into a long-form using the pandas function stack(). stack() mainly stacks the specified index
4 min read