Skip to content
Advertisement

Reorder subset of columns in pandas dataframe with natural sorting

I have the following dataframe:

JavaScript

and I would like to sort only the columns with the F in this way:

JavaScript

How could I do?

(edit) The columns with the “F” can vary both in quantity and in the values that follow the F (in my case I have about 100 columns like those) The columns with F are always grouped but the number before and after is variable

Advertisement

Answer

You can use natsort for natural sorting and a mask to handle only the F columns:

JavaScript

Alternative without natsort:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement