Python 多索引和单索引数据帧
我正在努力将2个数据帧相乘。如能帮助您理解错误,我们将不胜感激 第一个dfPython 多索引和单索引数据帧,python,pandas,Python,Pandas,我正在努力将2个数据帧相乘。如能帮助您理解错误,我们将不胜感激 第一个df base_currency quoted_currency BTC JPY 133.242020 USD 5.664089 ETH JPY 1252.367170 USD
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
将
添加到\u numpy
以消除索引的影响
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
您好,您能在代码块中共享预期的输出吗?@RichieV我已经编辑了to\u numpy()
为我抛出了一个错误<代码>重塑(-1,1)
修复了它。不确定对你来说是否相同。@AkshaySehgal你添加了['balance']