Pandas
keyboard_arrow_down 655 guides
chevron_leftMulti-index Operations Cookbook
check_circle
Mark as learned thumb_up
0
thumb_down
0
chat_bubble_outline
0
Comment auto_stories Bi-column layout
settings
Combining multiple DataFrames into one DataFrame in Pandas
schedule Aug 12, 2023
Last updated local_offer
Tags Python●Pandas
tocTable of Contents
expand_more Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!
Start your free 7-days trial now!
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?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!