You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
df=pd.DataFrame({"id":[6,5,4,3,2,1], "raw_grade":['a', 'b', 'b', 'a', 'a', 'e']})
df["grade"] =pd.Categorical(df["raw_grade"])
df['grade'].cat.reorder_levels(['b', 'e', 'a'])
# sorts 'grade' according to the order of the levelsdf.sort(columns=['grade'])
correct output
id raw_grade grade
4 5 b b
3 4 b b
0 1 e e
2 6 a a
1 3 a a
5 2 a a
In [1]: df = DataFrame({"id":[6,5,4,3,2,1], "raw_grade":['a', 'b', 'b', 'a', 'a', 'e']})
In [2]: df["grade"] = pd.Categorical(df["raw_grade"])
In [3]: df['grade'].cat.reorder_levels(['b', 'e', 'a'])
In [4]: df
Out[4]:
id raw_grade grade
0 6 a a
1 5 b b
2 4 b b
3 3 a a
4 2 a a
5 1 e e
In [5]: df.dtypes
Out[5]:
id int64
raw_grade object
grade category
dtype: object
In [6]: df.sort(columns=['grade'])
Out[6]:
id raw_grade grade
1 5 b b
2 4 b b
5 1 e e
0 6 a a
3 3 a a
4 2 a a
In [7]: df.sort(columns=['grade', 'id'])
Out[7]:
id raw_grade grade
2 4 b b
1 5 b b
5 1 e e
4 2 a a
3 3 a a
0 6 a a
code
correct output
code
wrong output
When there is more than one element in the columns list, the Categoricals columns are sorted lexically.
pandas: 0.14.1-78-g24b309f
The text was updated successfully, but these errors were encountered: