How to Concatenate Rows in pandas: Simple Guide
To concatenate rows in pandas, use
pd.concat() with a list of DataFrames and set axis=0. This stacks the rows vertically, combining data from multiple DataFrames into one.Syntax
The basic syntax to concatenate rows in pandas is:
pd.concat([df1, df2, ...], axis=0): Concatenates DataFrames vertically (row-wise).ignore_index=True: Resets the row index in the result.join='outer': Includes all columns from all DataFrames.
python
pd.concat([df1, df2, ...], axis=0, ignore_index=False, join='outer')
Example
This example shows how to concatenate two DataFrames row-wise using pd.concat(). The rows from both DataFrames are stacked, and the index is reset for a clean result.
python
import pandas as pd df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]}) result = pd.concat([df1, df2], axis=0, ignore_index=True) print(result)
Output
A B
0 1 3
1 2 4
2 5 7
3 6 8
Common Pitfalls
Common mistakes when concatenating rows include:
- Forgetting to set
axis=0(default is 0, but explicit is clearer). - Not using
ignore_index=True, which keeps original indexes and can cause duplicate index values. - Concatenating DataFrames with different columns without specifying
join, which may result in missing values.
python
import pandas as pd df1 = pd.DataFrame({'A': [1, 2]}) df2 = pd.DataFrame({'B': [3, 4]}) # Wrong: missing axis and ignore_index wrong = pd.concat([df1, df2]) # Right: specify axis and ignore_index right = pd.concat([df1, df2], axis=0, ignore_index=True, join='outer') print('Wrong concatenation:\n', wrong) print('\nRight concatenation:\n', right)
Output
Wrong concatenation:
A B
0 1.0 NaN
1 2.0 NaN
0 NaN 3.0
1 NaN 4.0
Right concatenation:
A B
0 1.0 NaN
1 2.0 NaN
2 NaN 3.0
3 NaN 4.0
Quick Reference
Summary tips for concatenating rows in pandas:
- Use
pd.concat()withaxis=0to stack rows. - Set
ignore_index=Trueto reset row indexes. - Use
join='outer'to keep all columns when DataFrames differ. - Check for missing values if columns don't match.
Key Takeaways
Use pd.concat with axis=0 to concatenate rows vertically.
Set ignore_index=True to reset the row index after concatenation.
Use join='outer' to include all columns from all DataFrames.
Be careful with DataFrames having different columns to avoid missing data.
Always check the result index and columns after concatenation.