'sorting rows in a pandas dataframe in a way which is not alphabetical
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.
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Community |
Solution 3 | Shane S |