Pandas ordered categorical data on exam grades 'D',…,'A+'

北城余情 提交于 2019-12-24 00:45:41

问题


I have the following data in pandas, I was surprized that the output was: D+ A I was expecting A+ D

can someone explain please

df = pd.DataFrame(['A+','A','A-','B+','B','B-','C+','C','C-','D+','D'],
                     index = ['excellent','excellent','excellent','good','good','good','ok','ok','ok','poor','poor'])
df.rename (columns={0:'Grades'},inplace=True)
grades = df['Grades'].astype('category', categories = ['D','D+', 'C-', 'C','C+','B-','B','B+','A-','A','A+'],ordered=True)
print(max(grades),min(grades))

> D+ A

回答1:


max is a Python function and it does not respect the category ordering. It uses lexicographical ordering based on unicode codes.

If you want to take into account categorical orders, you need to use the methods defined on Series/DataFrames:

print(grades.min(), grades.max())

yields

D A+


来源:https://stackoverflow.com/questions/45766829/pandas-ordered-categorical-data-on-exam-grades-d-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!