Python – Pandas dataframe.append()
Last Updated :
21 Nov, 2024
Pandas append function is used to add rows of other dataframes to end of existing dataframe, returning a new dataframe object. Columns not in the original data frames are added as new columns and the new cells are populated with NaN value.
Append Dataframe into another Dataframe
In this example, we are creating two dataframes and append the second to the first one, using df.append().
Python
# 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]})
# to append df2 at the end of df1 dataframe
print(df1.append(df2))
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 Pandas 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.
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.
Python
# 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 Rows to Dataframe
In this example, we are appending dictionary as row to dataframe.
Python
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 to 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.
Python
# 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.
Python – Pandas dataframe.append() – FAQs
What is the append method in Pandas DataFrame?
The append() method in Pandas DataFrame is used to append rows of one DataFrame to another DataFrame. It returns a new DataFrame with the appended rows.
How to append a DataFrame to another DataFrame?
You can use the ‘append()’ method in Pandas to append one DataFrame to another. Here’s how:
import pandas as pd
# Example DataFrames
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
# Append df2 to df1
df_appended = df1.append(df2, ignore_index=True)
print(df_appended)
Output:
A B
0 1 3
1 2 4
2 5 7
3 6 8
‘ignore_index=True’ ensures that the index is reset after appending, giving a continuous range of indices.
How to append results to a DataFrame in Pandas?
If you want to append a single row or multiple rows to an existing DataFrame, you can use the loc accessor with a new index:
import pandas as pd
# Existing DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
# Append a new row
new_row = {'A': 5, 'B': 6}
df = df.append(new_row, ignore_index=True)
print(df)
Output:
A B
0 1 3
1 2 4
2 5 6
How to append a list in a DataFrame?
If you have a list and want to append it as a new row in a DataFrame, you can convert the list to a DataFrame and then append it:
import pandas as pd
# Existing DataFrame
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
# List to append
new_data = [5, 6]
# Convert list to DataFrame and append
df = df.append(pd.Series(new_data, index=df.columns), ignore_index=True)
print(df)
Output:
A B
0 1 3
1 2 4
2 5 6