更正pandas索引的排序顺序

问题描述 投票:1回答:2

我有一个如下所示的数据框。我的Date字段是dtype datetime64[ns]

           symbol        high         low
Date                                      
2018-08-16     spy  285.040009  283.359985
2018-08-17     spy  285.559998  283.369995
2018-08-16    nflx  331.170013  321.209991
2018-08-17    nflx  324.369995  312.959991
2017-07-17     spy  245.910004  245.330002
2017-07-18     spy  245.720001  244.669998

我的目标是首先通过symbol设置索引,然后通过Date设置索引,如下所示:

                          high         low
symbol Date 
spy     2017-07-17  245.910004  245.330002
        2017-07-18  245.720001  244.669998                             
        2018-08-16  285.040009  283.359985
        2018-08-17  285.559998  283.369995
nflx    2018-08-16  331.170013  321.209991
        2018-08-17  324.369995  312.959991

以下是我的尝试:通过执行重置Date索引后,输出如下所示:

df.reset_index(level=['Date'], inplace=True)

        Date symbol        high         low
0 2018-08-16     spy  285.040009  283.359985
1 2018-08-17     spy  285.559998  283.369995
2 2018-08-16    nflx  331.170013  321.209991
3 2018-08-17    nflx  324.369995  312.959991
4 2017-07-17     spy  245.910004  245.330002
5 2017-07-18     spy  245.720001  244.669998

最后在符号和Date上设置索引,返回不需要的输出:

df.set_index(['symbol', 'Date'], inplace=True)

                          high         low
symbol Date                              
spy     2018-08-16  285.040009  283.359985
        2018-08-17  285.559998  283.369995
nflx    2018-08-16  331.170013  321.209991
        2018-08-17  324.369995  312.959991
spy     2017-07-17  245.910004  245.330002
        2017-07-18  245.720001  244.669998
python pandas numpy dataframe
2个回答
1
投票

IIUC,您可以尝试使用swaplevel,然后使用sort_index

df.set_index('symbol', append=True).swaplevel().sort_index(level=[0,1],ascending=[False,True])

                         high         low
symbol Date                              
spy    2017-07-17  245.910004  245.330002
       2017-07-18  245.720001  244.669998
       2018-08-16  285.040009  283.359985
       2018-08-17  285.559998  283.369995
nflx   2018-08-16  331.170013  321.209991
       2018-08-17  324.369995  312.959991

1
投票

不是inplace的粉丝,但试试pd.sort_index()

df.reset_index(level=['Date'], inplace= True)
df.set_index(['symbol', 'Date'], inplace=True)
print(df.sort_index())

输出:

                        high         low
symbol Date                              
nflx   2018-08-16  331.170013  321.209991
       2018-08-17  324.369995  312.959991
spy    2017-07-17  245.910004  245.330002
       2017-07-18  245.720001  244.669998
       2018-08-16  285.040009  283.359985
       2018-08-17  285.559998  283.369995
© www.soinside.com 2019 - 2024. All rights reserved.