在选定级别上将两个单索引数据框与多索引数据框相乘

时间:2020-08-20 06:59:51

标签: python pandas

我想在base_currency级别上将df1乘以df2。 df1索引中有重复项。尝试以下代码时出现错误:

df1.loc[df1['total'].mul(df2['balance'])]

任何帮助将不胜感激。我不知道什么是最好的方法。我应该在df1中保留单索引base_currency还是将其更改为多索引[base_currency, quoted_currency]

df1:

               quoted_currency     total
base_currency                                
BTC                       USD    1.496949e+00
BTC                       EUR    2.889100e-03
BTC                       JPY    1.328009e+04
BTC                       AUD    5.683850e-03
ETH                       USD    3.407804e+01
ETH                       JPY    2.532950e+03
ETH                       BTC    6.235470e+01
BCH                       USD    7.352000e-02
BCH                       JPY    1.637898e+02
CASH                      JPY    3.304434e+05
CASH                      ETH    1.218826e+02
CASH                      BTC    1.314054e+05
CASH                      USD    1.155685e+03
CASH                      EUR    1.159377e+03
CASH                      AUD    1.111565e+03
XRP                       JPY    6.699868e+06
XRP                       USD    1.859000e+02
XRP                      QASH    1.215415e+03
XRP                       BTC    1.111366e+04
BCH                       BTC    6.317441e+01

df2:

              balance
currency              
BCH       3.110515e+04
BTC       1.249170e+06
ETH       4.332074e+04
XRP       3.081000e+01
CASH      4.565000e+00
AUD       7.104848e-01
CNY       1.433651e-01
EUR       1.174428e+00
HKD       1.283253e-01
IDR       5.674280e-05
INR       1.334581e-02
JPY       9.441869e-03
PHP       2.033362e-02
SGD       7.227208e-01
USD       1.000000e+00

2 个答案:

答案 0 :(得分:1)

以下两种解决方案都是可能的:

使用MultiIndex更简单,将level=0参数添加到Series.mul

df11 = df1.set_index('quoted_currency', append=True)

s = df11['total'].mul(df2['balance'], level=0)
print (s)
base_currency  quoted_currency
BTC            USD                1.869944e+06
               EUR                3.608977e+03
               JPY                1.658909e+10
               AUD                7.100095e+03
ETH            USD                1.476286e+06
               JPY                1.097293e+08
               BTC                2.701252e+06
BCH            USD                2.286851e+03
               JPY                5.094706e+06
CASH           JPY                1.508474e+06
               ETH                5.563941e+02
               BTC                5.998657e+05
               USD                5.275702e+03
               EUR                5.292556e+03
               AUD                5.074294e+03
XRP            JPY                2.064229e+08
               USD                5.727579e+03
               QASH               3.744694e+04
               BTC                3.424119e+05
BCH            BTC                1.965049e+06
dtype: float64

在没有MultiIndexIndex.intersectionSeries.reindex的情况下:

idx = df1.index.intersection(df2.index).unique()
s = df1['total'].mul(df2['balance'].reindex(idx))
print (s)
base_currency
BTC     1.869944e+06
BTC     3.608977e+03
BTC     1.658909e+10
BTC     7.100095e+03
ETH     1.476286e+06
ETH     1.097293e+08
ETH     2.701252e+06
BCH     2.286851e+03
BCH     5.094706e+06
CASH    1.508474e+06
CASH    5.563941e+02
CASH    5.998657e+05
CASH    5.275702e+03
CASH    5.292556e+03
CASH    5.074294e+03
XRP     2.064229e+08
XRP     5.727579e+03
XRP     3.744694e+04
XRP     3.424119e+05
BCH     1.965049e+06
dtype: float64

答案 1 :(得分:1)

Index.mapSeries.mul一起使用:

s = df1['total'].mul(df1.index.map(df2['balance']))

结果:

base_currency
BTC     1.869944e+06
BTC     3.608977e+03
BTC     1.658909e+10
BTC     7.100095e+03
ETH     1.476286e+06
ETH     1.097293e+08
ETH     2.701252e+06
BCH     2.286851e+03
BCH     5.094706e+06
CASH    1.508474e+06
CASH    5.563941e+02
CASH    5.998657e+05
CASH    5.275702e+03
CASH    5.292556e+03
CASH    5.074294e+03
XRP     2.064229e+08
XRP     5.727579e+03
XRP     3.744694e+04
XRP     3.424119e+05
BCH     1.965049e+06
dtype: float64