Append DataFrame to the end of Other DataFrame
To append rows of a DataFrame to the end of this DataFrame, call append() method on this DataFrame and pass the other DataFrame as parameter to this function.
In this tutorial, we will learn the syntax of DataFrame.append() method and how to use this function to append a DataFrame using specified operation(s).
Syntax
The syntax of pandas DataFrame.append() method is
DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)
where
Parameter | Value | Description |
---|---|---|
other | DataFrame or Series/dict-like object, or list of these | The data to append. |
ignore_index | bool, default False | If True, the resulting axis will be labeled 0, 1, …, n – 1. |
verify_integrity | bool, default False | If True, raise ValueError on creating index with duplicates. |
sort | bool, default False | Sort columns if the columns of self and other are not aligned. Changed in version 1.0.0: Changed to not sort by default. |
Return Value
DataFrame.
Examples
Append a DataFrame to another DataFrame with Same Index
In the following program, we take a DataFrame and append it using the “other” property, where both the dataframes are given same index.
Example.py
import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'), index=['x', 'y'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y'])
result = df.append(df2)
print(result)
Output
A B
x 1 2
y 3 4
x 5 6
y 7 8
Append DataFrame Ignoring the Index
In the following program, we take a DataFrame and append it by passing ignore_index =’True’.
Example.py
import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'), index=['x', 'y'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'), index=['x', 'y'])
result = df.append(df2, ignore_index=True)
print(result)
Output
A B
0 1 2
1 3 4
2 5 6
3 7 8
Generate a DataFrame in a Loop using append() Function
The following program is a less efficient way of generating a dataframe.
Example.py
import pandas as pd
df = pd.DataFrame(columns=['A'])
for i in range(5):
df = df.append({'A': i}, ignore_index=True)
print(df)
Output
A
0 0
1 1
2 2
3 3
4 4
Conclusion
In this Pandas Tutorial, we learned how to append a DataFrame to this DataFrame using pandas DataFrame.append() method.