chevron_left
Multi-index Operations Cookbook
0
0
0
new
Combining multiple DataFrames into one DataFrame in Pandas
Programming
chevron_rightPython
chevron_rightPandas
chevron_rightCookbooks
chevron_rightDataFrame Cookbooks
chevron_rightMulti-index Operations Cookbook
schedule Mar 10, 2022
Last updated Python●Pandas
Tags tocTable of Contents
expand_more To combine multiple DataFrames into a single DataFrame, use the pd.concat(~)
method.
Examples
Consider the following DataFrame:
df
A B0 2 41 3 5
Here's the other DataFrame we want to concatenate:
df_other
A B0 6 81 7 9
Notice how df
and df_other
have matching column labels.
Concatenating DataFrames vertically
To concatenate multiple DataFrames vertically:
A B0 2 41 3 50 6 81 7 9
Concatenating DataFrames horizontally
To concatenate multiple DataFrames horizontally, pass in axis=1
like so:
A B A B0 2 4 6 81 3 5 7 9
Case when column labels differ
Consider the following DataFrame:
df
A B0 2 41 3 5
Here's the other DataFrame we want to concatenate:
df_other
B C0 6 81 7 9
Concatenating them vertically:
A B C0 2.0 4 NaN1 3.0 5 NaN0 NaN 6 8.01 NaN 7 9.0
Observe how the two DataFrames got vertically stacked with shared column (B
) aligned. The newly introduced gaps are then filled using nan
.
NOTE
To learn more about concat(~)
, check our our full documentation.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...