Pandas透视表手动排序列

20

给定一个数据框:

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

https://dev59.com/_GYs5IYBdhLWcg3wGf_F - Sruthi
这里有一个答案可以帮到您:https://dev59.com/_GYs5IYBdhLWcg3wGf_F - Yoann boyere
1个回答

12

您可以使用:

df = df.reindex_axis(['testc','testd', 'testa','testb'], axis=1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接