Append rows to a Pandas DataFrame

Python

Pandas

Join

Append rows

Luc B.

Python

Pandas

DataFrame

illustration of row concatenation

Appending rows to a DataFrame is a special case of concatenation in which there are only two DataFrames. Row concatenation is useful if, for example, data are spread across multiple files but have the same structure (i.e. all files have the same columns).

Code Example

Use the pd.append() function to append the rows of one DataFrame to another.

import pandas as pd

df1 = pd.DataFrame({
    'column1': [1, 2, 3, 4]
})

df2 = pd.DataFrame({
    'column1': [5, 6, 7, 8]
})

# Append rows here
df1.append(df2)
column1
0 1
1 2
2 3
3 4
0 5
1 6
2 7
3 8

Note in the example above, the resulting DataFrame preserves the index values of the original DataFrames. However, by using the ignore_index parameter, Pandas will assign new index values to the resulting DataFrame that range from 0 to n.

# Apend rows and create new unique index values
df1.append(df2, ignore_index=True)
column1
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8

To perform more general row concatenation in Pandas, take a look at this article