简体   繁体   English

Python Pandas:如何添加多索引的另一个名称?

[英]Python Pandas: How to add another name of multiindex?

I was playing around crypto data in pandas.我在 pandas 中玩弄加密数据。 After merging several dataframes, I got this合并几个数据框后,我得到了这个

                                            
       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

I want to add another level of index on top, so it would be like我想在顶部添加另一个级别的索引,所以就像

                      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

So it will be easy to me to add more columns like this:所以我很容易添加更多这样的列:

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()

Can someone advise?有人可以建议吗? Thanks.谢谢。

You can create a MultiIndex like this in a couple of ways:您可以通过以下几种方式创建这样的MultiIndex

new_columns = pd.MultiIndex.from_arrays([
    (["btc"] * 5) + (["eth"] * 5), 
    df.columns[1:] # exclude "timestamp" from our new columns
])

new_df = df.set_index("timestamp").set_axis(new_columns, axis=1)

print(new_df)
                    btc                                              eth
                   open      high       low     close   volume      open      high      low     close     volume
timestamp
1620202740000  54945.31  54987.01  54945.30  54978.49  118.239  54945.31  54987.01  54945.3  54978.49   4345.000
1620202800000  54978.49  55054.00  54972.04  55027.12  337.619  54945.31  54987.01  54945.3  54978.49    134.239
1620202860000  55027.12  55041.05  54950.05  54951.96  131.414  54945.31  54987.01  54945.3  54978.49  14358.239
1620202920000  54951.96  55067.36  54951.95  55063.78  176.529  54945.31  54987.01  54945.3  54978.49   1148.239
1620202980000  55063.79  55064.00  55000.00  55014.39  107.082  54945.31  54987.01  54945.3  54978.49     18.239

Alternatively, you can use MultiIndex.from_product like so:或者,您可以像这样使用MultiIndex.from_product

new_columns = pd.MultiIndex.from_product([
    ["btc", "eth"], 
    ["open", "high", "low", "close", "volume"]
])

# same as above
new_df = df.set_index("timestamp").set_axis(new_columns, axis=1)

Just for completeness, if you split the columns with expand=True , they will be expanded into a MultiIndex :为了完整起见,如果您使用expand=True拆分列,它们将扩展为MultiIndex

df = df.set_index('timestamp')
df.columns = [pre+col for pre,col in zip(['btc_']*5 + ['eth_']*5, df.columns)]
df.columns = df.columns.str.split('_', expand=True)

#                     btc                                              eth
#                    open      high       low     close   volume      open      high      low     close     volume
# timestamp
# 1620202740000  54945.31  54987.01  54945.30  54978.49  118.239  54945.31  54987.01  54945.3  54978.49   4345.000
# 1620202800000  54978.49  55054.00  54972.04  55027.12  337.619  54945.31  54987.01  54945.3  54978.49    134.239
# 1620202860000  55027.12  55041.05  54950.05  54951.96  131.414  54945.31  54987.01  54945.3  54978.49  14358.239
# 1620202920000  54951.96  55067.36  54951.95  55063.78  176.529  54945.31  54987.01  54945.3  54978.49   1148.239
# 1620202980000  55063.79  55064.00  55000.00  55014.39  107.082  54945.31  54987.01  54945.3  54978.49     18.239

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM