veeresh kanaboor
veeresh kanaboor

Reputation: 3

Re-ordering columns in pandas dataframe based on column header names, where the name of the columns are string, contains the numeric number at the end

enter image description here Before sorting:

If we look at the file, the columns are unsorted. Example column headers are L0_S0_F0, L0_S0_F4, L0_S0_F2, L0_S0_F10, L0_S0_F6, L0_S0_F8 and L0_S0_F12 respectively

File after merging

enter image description here: After sorting properly, We need to have data frame with sequentially ordered columns with their order L0_S0_F0, L0_S0_F2, L0_S0_F4, L0_S0_F6, L0_S0_F8, L0_S0_F10 and L0_S0_F12 respectively

Here the column name, example L0_S0_F0, which represents Line 0, station 0 and feature 2. Now we need to sort the columns based on the name of the features( digit after 'F').

File after reordering

enter image description here

Upvotes: 0

Views: 380

Answers (2)

veeresh kanaboor
veeresh kanaboor

Reputation: 3

df=num_cat_merged_raw_25.drop(['Id','Response'],axis='columns') df = df.reindex(sorted(df.columns[0:], key=lambda x: int(x.split('_')[-1][1:])), axis=1)

This code worked for me. Thank you Pygirl, for driving me towards the logic :)

Upvotes: 0

Pygirl
Pygirl

Reputation: 13349

It will be :

df = df.reindex(sorted(df.columns[1:], key=lambda x: int(x.split('_')[-1][1:])), axis=1)

Upvotes: 1

Related Questions