【发布时间】:2021-07-28 00:33:06
【问题描述】:
我在玩 Pandas 中的加密数据。合并几个数据框后,我得到了这个
timestamp open high low close volume open high low close volume
0 1620202740000 54945.31 54987.01 54945.30 54978.49 118.239 54945.31 54987.01 54945.30 54978.49 4345
1 1620202800000 54978.49 55054.00 54972.04 55027.12 337.619 54945.31 54987.01 54945.30 54978.49 134.239
2 1620202860000 55027.12 55041.05 54950.05 54951.96 131.414 54945.31 54987.01 54945.30 54978.49 14358.239
3 1620202920000 54951.96 55067.36 54951.95 55063.78 176.529 54945.31 54987.01 54945.30 54978.49 1148.239
4 1620202980000 55063.79 55064.00 55000.00 55014.39 107.082 54945.31 54987.01 54945.30 54978.49 18.239
我想在顶部添加另一个级别的索引,所以它会像
btc btc btc btc btc eth eth eth eth eth
timestamp open high low close volume open high low close volume
0 1620202740000 54945.31 54987.01 54945.30 54978.49 118.239 54945.31 54987.01 54945.30 54978.49 4345
1 1620202800000 54978.49 55054.00 54972.04 55027.12 337.619 54945.31 54987.01 54945.30 54978.49 134.239
2 1620202860000 55027.12 55041.05 54950.05 54951.96 131.414 54945.31 54987.01 54945.30 54978.49 14358.239
3 1620202920000 54951.96 55067.36 54951.95 55063.78 176.529 54945.31 54987.01 54945.30 54978.49 1148.239
4 1620202980000 55063.79 55064.00 55000.00 55014.39 107.082 54945.31 54987.01 54945.30 54978.49 18.239
所以我很容易像这样添加更多列:
for x in ['btc', 'eth']:
df.loc[:, (x, 'fast_ema_1min')] = df[x]['close'].rolling(window=1).mean()
df.loc[:, (x, 'slow_ema_20min')] = df[x]['close'].rolling(window=20).mean()
有人可以建议吗?谢谢。
【问题讨论】:
标签: python pandas indexing multi-index