Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to append two DataFrames in Pandas?
To append the rows of one DataFrame with the rows of another, we can use the Pandas append() function. With the help of append(), we can combine DataFrames vertically. Let's see how to use this method with examples.
Note: The append() method is deprecated since Pandas 1.4.0. Use pd.concat() instead for new code.
Steps to Append DataFrames
- Create two DataFrames with data
- Use
append()method orpd.concat()to combine them - Set
ignore_index=Trueto reset row indices - Handle different column names appropriately
Example 1: Appending DataFrames with Same Columns
When DataFrames have the same column names, appending works smoothly ?
import pandas as pd
df1 = pd.DataFrame({"x": [5, 2], "y": [4, 7], "z": [9, 3]})
df2 = pd.DataFrame({"x": [1, 3], "y": [1, 9], "z": [29, 30]})
print("Input DataFrame 1 is:")
print(df1)
print("\nInput DataFrame 2 is:")
print(df2)
df3 = df1.append(df2, ignore_index=True)
print("\nAfter appending, DataFrame is:")
print(df3)
Input DataFrame 1 is: x y z 0 5 4 9 1 2 7 3 Input DataFrame 2 is: x y z 0 1 1 29 1 3 9 30 After appending, DataFrame is: x y z 0 5 4 9 1 2 7 3 2 1 1 29 3 3 9 30
Example 2: Appending DataFrames with Different Columns
When DataFrames have different column names, missing values are filled with NaN ?
import pandas as pd
df1 = pd.DataFrame({"x": [5, 2], "y": [4, 7], "z": [9, 3]})
df2 = pd.DataFrame({"a": [1, 3], "b": [1, 9], "c": [29, 30]})
print("Input DataFrame 1 is:")
print(df1)
print("\nInput DataFrame 2 is:")
print(df2)
df3 = df1.append(df2)
print("\nAfter appending, DataFrame is:")
print(df3)
Input DataFrame 1 is:
x y z
0 5 4 9
1 2 7 3
Input DataFrame 2 is:
a b c
0 1 1 29
1 3 9 30
After appending, DataFrame is:
x y z a b c
0 5.0 4.0 9.0 NaN NaN NaN
1 2.0 7.0 3.0 NaN NaN NaN
0 NaN NaN NaN 1.0 1.0 29.0
1 NaN NaN NaN 3.0 9.0 30.0
Modern Approach: Using pd.concat()
The recommended way to append DataFrames is using pd.concat() ?
import pandas as pd
df1 = pd.DataFrame({"x": [5, 2], "y": [4, 7], "z": [9, 3]})
df2 = pd.DataFrame({"x": [1, 3], "y": [1, 9], "z": [29, 30]})
# Using pd.concat() instead of append()
df3 = pd.concat([df1, df2], ignore_index=True)
print("Using pd.concat():")
print(df3)
Using pd.concat(): x y z 0 5 4 9 1 2 7 3 2 1 1 29 3 3 9 30
Key Parameters
-
ignore_index=True- Resets row indices to 0, 1, 2... -
sort=False- Prevents automatic column sorting -
verify_integrity=True- Checks for duplicate indices
Conclusion
Use pd.concat() for appending DataFrames as append() is deprecated. Set ignore_index=True to reset indices. DataFrames with different columns will have NaN values in missing positions.
