乘以multiindex和signle索引数据框

时间:2020-09-04 01:59:55

标签: python pandas

我正努力将2个数据帧相乘。对于理解该错误的任何帮助将不胜感激。

第一个df

base_currency  quoted_currency
BTC            JPY                   133.242020
               USD                     5.664089
ETH            JPY                  1252.367170
               USD                     0.060000
XRP            JPY                758521.049895
Name: open_position_short, dtype: float64

第二个df

               balance
currency              
BCH       3.089170e+04
BTC       1.264052e+06
ETH       5.039736e+04
XRP       3.123000e+01
QASH      4.825000e+00
AUD       7.814072e+01
CNY       1.547688e+01
EUR       1.264706e+02
HKD       1.366586e+01
IDR       6.201115e-03
INR       1.443351e+00
JPY       1.000000e+00
PHP       2.185865e+00
SGD       7.785641e+01
USD       1.059965e+02

代码:

new = df1.mul(df2.reindex(df1.index.get_level_values('base_currency')))

错误:

    raise ValueError("cannot join with no overlapping index names")
ValueError: cannot join with no overlapping index names

预期输出:

base_currency  quoted_currency
BTC            JPY                1.684248e+08
               USD                7.159703e+06
ETH            JPY                6.311600e+07
               USD                3.023842e+03
XRP            JPY                2.368861e+07

1 个答案:

答案 0 :(得分:1)

添加to_numpy以消除index的影响

df1 *= df2.reindex(df1.index.get_level_values('base_currency'))['balance'].to_numpy()
df1
Out[78]: 
base_currency  quoted_currency
BTC            JPY                1.684248e+08
               USD                7.159703e+06
ETH            JPY                6.311600e+07
               USD                3.023842e+03
XRP            JPY                2.368861e+07
Name: c, dtype: float64