I have a DataFrame called by_country. In this DF, I have multiple columns, first half is the total number of answers by age, the other half is the number of answers 'yes' by age.
I am going to reorder the total and 'yes' answer side by side for every different age group. Is there easier way to do this without typing columns manually?
Here is my columns:
by_country.columns = ['count_18-24', 'count_25-34', 'count_35-44', 'count_45-54',
'count_55-64', 'count_65', 'count_no_reply', 'count_18_under',
'sum_18-24', 'sum_25-34', 'sum_35-44', 'sum_45-54', 'sum_55-64',
'sum_65', 'sum_no_reply', 'sum_18_under']
I want to order like
by_country.columns = ['count_18-24','sum_18-24', 'count_25-34','sum_25-34', 'count_35-44', 'sum_35-44','count_45-54','sum_45-54', 'count_55-64','sum_55-64','count_65', 'sum_65','count_no_reply', 'sum_no_reply', 'count_18_under','sum_18_under']
I have similar problem with other bigger DataFrame and it is hard to type them all. Any suggestion and advice is highly appreciated!
CodePudding user response:
Seems like you can just sort the columns using sorted
and a key that takes the last _
-separated item:
by_country.columns = sorted(by_country.columns, key=lambda x: x.split('_')[-1])