when I want to sort one of the columns with a specific order, there is the only way to do this right now. example, we have this dataframe
import pandas as pd
df = pd.DataFrame({'good': [10], 'excellent': [20], 'bad': [10]})
df.head()
bad excellent good
0 10 20 10
and If I want to sort the columns with the good excellent bad
order, there is the only way
pd.DataFrame(df, columns=['good', 'excellent', 'bad'])
good excellent bad
0 10 20 10
but this is ugly.
can you consider this way of solution?
import pandas as pd
df = pd.DataFrame({'good': [10], 'excellent': [20], 'bad': [10]})
df.sort_columns(specific_order=['good', excellent', 'bad'])
good excellent bad
0 10 20 10
Comment From: sinhrks
I think the usecase is limited, and the following is simpler:
df[['good', 'excellent', 'bad']]
Or, using orderd Categorical
as columns meets your requirement.
- http://pandas.pydata.org/pandas-docs/stable/categorical.html#sorting-and-order
Finally, an generic idea is adding new arg like standard sort
's key
arg, not sure how often it'll be used.
Comment From: jreback
@SamuraiT you can do this:
In [3]: df.reindex(columns=['excellent', 'good', 'bad'])
Out[3]:
excellent good bad
0 20 10 10
or use a Categorical
as @sinhrks suggest.
Comment From: SamuraiT
reindex
is a good solution, thanks :D