Pandas Pivot表手动排序列[重复]

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

对于给定的数据框:

UUT                  testa  testb  testc  testd
DateTime                                
2017-11-21 18:47:29    1.0    1.0    1.0    3.0
2017-11-21 18:47:30    1.0    2.0    1.0    4.0
2017-11-21 18:47:31    1.0    2.0    5.0    2.0
2017-11-21 18:47:32    1.0    2.0    5.0    1.0
2017-11-21 18:47:33    1.0    2.0    5.0    4.0
2017-11-21 18:47:34    1.0    2.0    5.0    1.0

如果我想要以下顺序,我怎么能手动重新排列我想要的列?

testc, testd, testa, testb

因此表格和情节将以这种方式:

UUT                  testc  testd  testa  testb  
DateTime                                         
2017-11-21 18:47:29    1.0    3.0    1.0    1.0  
2017-11-21 18:47:30    1.0    4.0    1.0    2.0  
2017-11-21 18:47:31    5.0    2.0    1.0    2.0  
2017-11-21 18:47:32    5.0    1.0    1.0    2.0  
2017-11-21 18:47:33    5.0    4.0    1.0    2.0  
2017-11-21 18:47:34    5.0    1.0    1.0    2.0
python pandas sorting pivot-table
1个回答
5
投票

您可以使用:

df = df.reindex_axis(['testc','testd', 'testa','testb'], axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.