Data Manipulation - Sort Index when values are Alphanumeric

前端 未结 1 1265
轮回少年
轮回少年 2021-01-23 06:56

I\'m wondering how I should approach this data manipulation predicament. What is the best method to sort an index of a multi-index in a data frame where the values of on level o

相关标签:
1条回答
  • 2021-01-23 07:18

    You can use the natsort package to naturally sort your columns. Here's an example:

    import natsort as ns
    
    c =  ['0', '1', '10', ...]
    c = sorted(ns.natsorted(c), key=lambda x: not x.isdigit())
    
    print(c)
    ['0',
     '1',
     '2',
     '3',
     '4',
     '5',
     '6',
     '7',
     '8',
     '9',
     '10',
     '11',
     '2Y',
     '3Y',
     '4Y',
     '5Y',
     '9Y']
    

    For your problem, a similar approach follows with reindex_axis as the extra step:

    c = df.columns.levels[1]
    c = sorted(ns.natsorted(c), key=str.isdigit, reverse=True)
    
    df = df.reindex_axis(pd.MultiIndex.from_product([df.columns.levels[0], c]), axis=1)
    
    0 讨论(0)
提交回复
热议问题