问题
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