Reputation: 1430
I have a dataframe with header that is list of 'string-integers':
import pandas as pd
d = {'1': [1, 2], '7': [3, 4], '3': [3, 4], '5': [2, 7]}
df = pd.DataFrame(data=d)
1 3 5 7
0 1 3 2 3
1 2 4 7 4
This code change order of column (sort):
cols = df.columns.tolist()
cols = [int(x) for x in cols]
cols.sort()
cols = [str(x) for x in cols]
df = df[cols]
1 3 5 7
0 1 3 2 3
1 2 4 7 4
I'm not happy with this solution. Of course, I can hide it in the function, but probably more elegant approach exist.
Upvotes: 1
Views: 127
Reputation: 164773
There are several options depending on what you require.
Option 1
You can use sort_values
to sort as strings:
df.columns = df.columns.sort_values()
Note this means that "10" will appear before "2".
Option 2
If you wish to convert to integers and then sort:
df.columns = df.columns.astype(int).sort_values()
Option 3
If you want to keep as string, but order by integer value:
df.columns = df.columns.astype(int).sort_values().astype(str)
A pure Python approach is also possible:
df.columns = sorted(df, key=int)
Upvotes: 1