Home > Net >  How to fix the order of columns in pandas dataframe?
How to fix the order of columns in pandas dataframe?

Time:10-25

Hi I have a dataframe in which I have to fix the order of columns. I have a list in which prefix of column name defined. I have the fix the according to that list.

Example:

df Columns :-

nOffered_count, nOffered_sum, nTransferred_count, nTransferred_sum, tacd_count, tacd_sum, tacw_count, tacw_sum, tHeld_count, tHeld_sum

I have a list -

list = ['nOffered', 'tacw', 'tacd', 'nTransferred', 'tHeld']

In result I want below order in dataframe:

nOffered_count, nOffered_sum, tacw_count, tacw_sum, tacd_count, tacd_sum, nTransferred_count, nTransferred_sum, , tHeld_count, tHeld_sum

CodePudding user response:

Create dictionary for ordering in enumerate and then sorting values before _ by mapping this dictionary:

L = ['nOffered', 'tacw', 'tacd', 'nTransferred', 'tHeld']
d = {v: k for k, v in dict(enumerate(L)).items()}

cols = sorted(df.columns, key = lambda x: d.get(x.split('_')[0], ''))
print(cols)
['nOffered_count', 'nOffered_sum', 'tacw_count', 'tacw_sum', 
 'tacd_count', 'tacd_sum', 'nTransferred_count', 'nTransferred_sum', 
 'tHeld_count', 'tHeld_sum']

Last change order in DataFrame:

df = df[cols]

Another idea:

df = df.sort_index(key = lambda x: x.str.split('_').str[0].map(d).fillna(''), axis=1)
print(df)
Empty DataFrame
Columns: [nOffered_count, nOffered_sum, tacw_count, tacw_sum, 
          tacd_count, tacd_sum, nTransferred_count, nTransferred_sum, 
          tHeld_count, tHeld_sum]
Index: []
  • Related