Skip to content Skip to sidebar Skip to footer

Data Manipulation - Sort Index When Values Are Alphanumeric

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 of th

Solution 1:

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)

Post a Comment for "Data Manipulation - Sort Index When Values Are Alphanumeric"