【问题标题】:Pandas Pivot Table manually sort columns [duplicate]熊猫数据透视表手动对列进行排序[重复]
【发布时间】:2023-03-09 21:54:02
【问题描述】:

对于给定的数据框:

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】:

你可以使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 2012-06-19
    • 2018-03-28
    • 2019-02-21
    相关资源
    最近更新 更多