Home > Software design >  How to attach 3 data frames horizontally and directly without using any joins?
How to attach 3 data frames horizontally and directly without using any joins?

Time:12-10

A data frame that contains only Country/Region column

I have a data frame that contains only the Country/Region column

The second data frame that contains only the Confirmed column

enter image description here

And the third data frame contains only the Deaths column

enter image description here

The question is how to directly attach these three data frame above without using any complicated method ?

I want the result like: enter image description here

CodePudding user response:

Try this:

new_df = pd.concat([df1, df2, df3])

CodePudding user response:

You need to do 2 pd.merges.

(1) Merge the first two (df1 and df2) on their indices

(2) Merge the dataframe from (1) with df3 on 'Country/Region'. Since the dataframe from (1) has 'Country/Region' as a column and df3 has it as index, you have to pass left_on=True, right_index=True:

out = df1.merge(df2, left_index=True, right_index=True).merge(df3, left_on='Country/Region', right_index=True)
  • Related