基于Python中另一个数据框的多个列合并和填充缺失值

时间:2019-10-31 03:09:50

标签: python pandas dataframe

为了合并基于yearcity的两个数据框,我想在 df1 gdp_valuegrowth_rate中填充缺失的值分别来自 df2 gdprate中的值。

df1

   year city  gdp_value  growth_rate
0  2015   sh        NaN          NaN
1  2016   sh        NaN          NaN
2  2017   sh        NaN          NaN
3  2018   sh        NaN          NaN
4  2019   sh        NaN          NaN
5  2015   bj        7.0         0.01
6  2016   bj        3.0         0.03
7  2017   bj        2.0        -0.03
8  2018   bj        5.0         0.05
9  2019   bj        4.0         0.02

df2

   year city  gdp  rate
0  2015   sh    6  0.04
1  2016   sh    5  0.07
2  2017   sh    3 -0.03
3  2018   sh    6  0.05
4  2019   sh    4  0.02

我尝试过pd.merge(df1, df2, on=['year', 'city'], how = 'left'),但得到了:

   year city  gdp_value  growth_rate  gdp  rate
0  2015   sh        NaN          NaN  6.0  0.04
1  2016   sh        NaN          NaN  5.0  0.07
2  2017   sh        NaN          NaN  3.0 -0.03
3  2018   sh        NaN          NaN  6.0  0.05
4  2019   sh        NaN          NaN  4.0  0.02
5  2015   bj        7.0         0.01  NaN   NaN
6  2016   bj        3.0         0.03  NaN   NaN
7  2017   bj        2.0        -0.03  NaN   NaN
8  2018   bj        5.0         0.05  NaN   NaN
9  2019   bj        4.0         0.02  NaN   NaN

我想要的输出 df 是这样的:

   year city  gdp_value  ratio_rate
0  2015   sh          6        0.04
1  2016   sh          5        0.07
2  2017   sh          3       -0.03
3  2018   sh          6        0.05
4  2019   sh          4        0.02
5  2015   bj          7        0.01
6  2016   bj          3        0.03
7  2017   bj          2       -0.03
8  2018   bj          5        0.05
9  2019   bj          4        0.02

感谢您的帮助。

已编辑,此解决方案似乎可行,谢谢:

df1 = df1.set_index(['year', 'city'])
df1.update(
    df2
    .set_index(['year', 'city'])\
    .rename(columns={'gdp':'gdp_value','rate':'growth_rate'})\
)
df1 = df1.reset_index()

1 个答案:

答案 0 :(得分:1)

如问题中所述,您还可以根据数据和需求使用更新:

df1 = df1.set_index(['year', 'city'])
df1.update(
    df2
    .set_index(['year', 'city'])\
    .rename(columns={'gdp':'gdp_value','rate':'growth_rate'})\
)
df1 = df1.reset_index()

一种方法是将combine_firstset_index一起使用并重命名列:

df1.set_index(['year','city'])\
   .combine_first(df2.set_index(['year','city'])
                     .rename(columns={'gdp':'gdp_value','rate':'growth_rate'}))\
   .reset_index()

输出:

   year city  gdp_value  growth_rate
0  2015   bj        7.0         0.01
1  2015   sh        6.0         0.04
2  2016   bj        3.0         0.03
3  2016   sh        5.0         0.07
4  2017   bj        2.0        -0.03
5  2017   sh        3.0        -0.03
6  2018   bj        5.0         0.05
7  2018   sh        6.0         0.05
8  2019   bj        4.0         0.02
9  2019   sh        4.0         0.02