Reputation: 21355
I have some dataframes (df) with categorical data starting with: a, b, c and a category for "remaining categories".
I would like to sort the month column in the dataframe ascending=true, but then have the category column sorted so that they are in the following order:
c
a
b
"remaining category"
Is this possible? --> Basically I want a custom sort order for a specific column, but then have the month column sorted in order of date.
Upvotes: 0
Views: 1762
Reputation: 2303
Pandas sort() was deprecated and removed from Pandas with release 0.20 (2017-05-05).
Pandas sort_values() and sort_index() are now used instead of sort(). To get a df in categorical order use pd.Categorical()
df = pd.DataFrame({'Panel':['Left', 'Right', 'Top', 'Bottom','Left'],
Value':[70, 50, 30, 40, 60]})
df['Panel'] = pd.Categorical(df['Panel'],
categories=['Top','Bottom','Left','Right'],ordered=True)
results
Panel Value
2 Top 30
3 Bottom 40
0 Left 70
4 Left 60
1 Right 50
Upvotes: 0
Reputation: 129018
docs are here
In [8]: df = DataFrame({'A' : [1,1,1,2,2,3], 'B' : list('bbcdae') })
In [9]: df.dtypes
Out[9]:
A int64
B object
dtype: object
In [10]: df['B'] = pd.Categorical(df['B'],categories=list('ghbaedfc'))
In [11]: df
Out[11]:
A B
0 1 b
1 1 b
2 1 c
3 2 d
4 2 a
5 3 e
In [12]: df.dtypes
Out[12]:
A int64
B category
dtype: object
In [13]: df.sort(['B','A'])
Out[13]:
A B
0 1 b
1 1 b
4 2 a
5 3 e
3 2 d
2 1 c
Upvotes: 1
Reputation: 8493
You can do it with a dict and adding a new 'sort' column to your dataframe. Check out this similar question Custom sorting with Pandas
Upvotes: 0