DataFrame.append() is used in Pandas to add rows from another DataFrame, Series, dictionary or list to the end of an existing DataFrame. It returns a new DataFrame and do not modify the original one.
Note: As of Pandas 2.0, DataFrame.append() has been removed. The recommended alternative is pd.concat().
Example: This example appends one DataFrame to another.
import pandas as pd
a = pd.DataFrame({"a":[1,2], "b":[3,4]})
b = pd.DataFrame({"a":[5,6], "b":[7,8]})
res = a.append(b)
print(res)
Output:
a b
0 1 3
1 2 4
0 5 7
1 6 8
Explanation: a.append(b) returns a new DataFrame by adding rows of a to b. The original index values are preserved.
Syntax
DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)
Parameters:
- other: DataFrame, Series, dict-like object, or list of these.
- ignore_index: If True, resets the index in the new DataFrame.
- verify_integrity: If True, raises an error for duplicate index values.
- sort: Sorts columns if they are not aligned.
Return Type: Returns a new DataFrame.
Examples
Example 1: This program creates two DataFrames and appends the second one to the first. The original index values are preserved.
import pandas as pd
a = pd.DataFrame({"a":[1,2,3], "b":[4,5,6]})
b = pd.DataFrame({"a":[7,8], "b":[9,10]})
r = a.append(b)
print(r)
Output
a b
0 1 4
1 2 5
2 3 6
0 7 9
1 8 10
Explanation: a.append(b) adds rows of a to b, keeping their original index values.
Example 2: This example appends two DataFrames and resets the index. It ensures a continuous index in the new DataFrame.
import pandas as pd
a = pd.DataFrame({"a":[1,2], "b":[3,4]})
b = pd.DataFrame({"a":[5], "b":[6]})
r = a.append(b, ignore_index=True)
print(r)
Output
a b
0 1 3
1 2 4
2 5 6
Explanation: ignore_index=True resets the index in the resulting DataFrame.
Example 3: This program appends two DataFrames with different column structures. Missing values are automatically filled with NaN.
import pandas as pd
a = pd.DataFrame({"a":[1,2], "b":[3,4]})
b = pd.DataFrame({"a":[5], "c":[7]})
r = a.append(b, ignore_index=True)
print(r)
Output
a b c
0 1.0 3.0 NaN
1 2.0 4.0 NaN
2 5.0 NaN 7.0
Explanation: When columns do not match, append() adds new columns and fills missing values with NaN.