I have a data frame that contains only the Country/Region column
The second data frame that contains only the Confirmed column
And the third data frame contains only the Deaths column
The question is how to directly attach these three data frame above without using any complicated method ?
CodePudding user response:
Try this:
new_df = pd.concat([df1, df2, df3])
CodePudding user response:
You need to do 2 pd.merge
s.
(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)