![]() |
VOOZH | about |
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.
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.
DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)
Parameters:
Return Type: Returns a new DataFrame.
Example 1: This program creates two DataFrames and appends the second one to the first. The original index values are preserved.
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.
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.
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.