根据其他数据框的值在一个数据框中填充值

Fill values in one dataframe depending on values other dataframe

我有两个数据框,如下所示

df1 = 
    date       balance    month    year    day     month_year
0   2019-10-07   0.0        10     2019      7    October-2019
1   2019-10-08   0.0        10     2019      8    October-2019
2   2019-10-09   0.0        10     2019      9    October-2019
3   2019-10-10   0.0        10     2019     10    October-2019
4   2019-10-11   0.0        10     2019     11    October-2019

我必须填写的数据框值:

df2 = 
day October-2019    November-2019   December-2019   January-2020    February-2020   March-2020  April-2020
 1      NaN             NaN              NaN              NaN          NaN          NaN           NaN
 2      NaN             NaN              NaN              NaN          NaN          NaN           NaN
 3      NaN             NaN              NaN              NaN          NaN          NaN           NaN
 ..     ...             ...              ...              ...          ...          ...           ...
 31     NaN             NaN              NaN              NaN          NaN          NaN           NaN

我必须根据 df1 中的 'month_year' 和 'day' 值,用 df1 中的 'balance' 值填充上面 df2 中的 NaN。

我已经尝试查找但失败了,寻求您的帮助。

使用DataFrame.pivot and then replace missing values by DataFrame.fillna or DataFrame.combine_first:

df11 = df1.pivot('day','month_year','balance')
df2 = df2.fillna(df11)

df11 = df1.pivot('day','month_year','balance')
df2 = df2.combine_first(df11)