python - 如何将 pandas 中具有不同索引的两列相乘?

标签 python pandas

我正在尝试将 pandas 数据框中的两列相乘并将其存储在新列中。我想按每天的市值(已排序)获取前 10 个值,并将 2017 年 1 月 1 日的供应量乘以 2017 年 1 月 2 日的价格。

df['New.Cap'] = df['Circulating.Supply'].loc['2017-01-01'].head(10) * df['Price'].loc['2017-02-01'].head(10)
print(df['New.Cap'])

我不断收到值错误:无法从重复轴重新索引。我认为这是由于当索引有重复值时加入/分配到列。

Data

最佳答案

我相信您需要创建MultiIndex,然后乘以由values创建的一维numpy数组:

注意 - 仅当符号的所有值都相同且两个日期时间的顺序相同时,解决方案才有效。

df = df.set_index('Symbo', append=True)

df['New.Cap'] = (df['Circulating.Supply'].loc['2017-01-01'] * 
                 df['Price'].loc['2017-02-01'].values)

如果不确定,则需要:

df = df.set_index('Symbo', append=True)

s = df['Circulating.Supply'].xs('2017-01-01') * df['Price'].xs('2017-02-01')
s.index = [np.repeat(pd.Timestamp('2017-01-01'), len(s)), s.index]
print (s)
2017-01-01  BTC      1.642702e+10
            DASH     8.258543e+07
            DGD               NaN
            DOGE     0.000000e+00
            ETC      1.259195e+08
            ETH      7.331980e+08
            FCT      2.608459e+07
            ICN      2.610000e+07
            LTC      2.285203e+08
            MAID     4.072972e+07
            REP      4.609000e+07
            STEEM    3.443731e+07
            WAVES    2.300000e+07
            XEM      0.000000e+00
            XMR      2.193754e+08
            XRP      3.633730e+08
            ZEC               NaN
dtype: float64
<小时/>
df['new'] = s
print (df)
                   Market.Cap    Price  Circulating.Supply           new
Date       Symbo                                                        
2017-01-01 BTC    16050414526   998.33        1.607734e+07  1.642702e+10
           ETH      715049311     8.17        8.749380e+07  7.331980e+08
           XRP      231408729     0.01        3.633730e+10  3.633730e+08
           LTC      221718486     4.51        4.914415e+07  2.285203e+08
           XMR      190983552    13.97        1.366825e+07  2.193754e+08
           ETC      122202804     1.40        8.744412e+07  1.259195e+08
           DASH      78524020    11.23        6.992839e+06  8.258543e+07
           REP       43994860     4.00        1.100000e+07  4.609000e+07
           MAID      43862003     0.10        4.525524e+08  4.072972e+07
           STEEM     36999610     0.16        2.295821e+08  3.443731e+07
           XEM       30962258     0.00        9.000000e+09  0.000000e+00
           ICN       26381302     0.30        8.700000e+07  2.610000e+07
           FCT       25392986     2.90        8.753219e+06  2.608459e+07
           DOGE      24501946     0.00        1.075440e+11  0.000000e+00
           WAVES     22132166     0.22        1.000000e+08  2.300000e+07
           DGD       18079172     9.04        2.000000e+06           NaN
2017-02-01 BTC    16429072864  1021.75        1.607930e+07           NaN
           ETH      733331862     8.38        8.752531e+07           NaN
           XRP      229312859     0.01        3.633730e+10           NaN
           LTC      228536428     4.65        4.915883e+07           NaN
           XMR      219509938    16.05        1.367528e+07           NaN
           ETC      125743676     1.44        8.747679e+07           NaN
           DASH      82595831    11.81        6.994957e+06           NaN
           REP       46101566     4.19        1.100000e+07           NaN
           MAID      42320210     0.09        4.525524e+08           NaN
           STEEM     35374447     0.15        2.296454e+08           NaN
           XEM       30396875     0.00        9.000000e+09           NaN
           ICN       26451766     0.30        8.700000e+07           NaN
           FCT       26119449     2.98        8.753219e+06           NaN
           DOGE      24343443     0.00        1.075570e+11           NaN
           WAVES     23267904     0.23        1.000000e+08           NaN
           ZEC       17773124    49.79        3.569940e+05           NaN

关于python - 如何将 pandas 中具有不同索引的两列相乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54476422/

相关文章:

Python:源代码字符串不能包含空字节

python - 按 T​​rue 序列分组

python-3.x - 我的 python 代码中有一个错误 key 错误 ['logo_url' ] 不在索引中

python-2.7 - 将任意数量的 pandas 列中的值组合成一个新列 - 非 SQL 意义上的 'join'

python - Numpy 或 pandas : test whether upper or lower bound is reached in array/series, 哪个先到达

python - ORM 对象值作为 Django 1.7 中模型字段的默认值

python - Paramiko SSH exec_command(shell脚本)在完成前返回

python - 根据 Pandas 中的另一列连接一组列值

python - 索引错误 : too many indices for array for an array that is definitely as big

python - 预取相关的django