多索引和单索引数据帧

2024-05-13 22:53:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力将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

Tags: dfbaseindex错误currencyethusdbtc
1条回答
网友
1楼 · 发布于 2024-05-13 22:53:43

添加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

相关问题 更多 >