本文介绍了 pandas 订购了有关考试成绩'D',...,'A +'的分类数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在熊猫中有以下数据,我感到惊讶的是输出为:D + A我期待着A + D
I have the following data in pandas, I was surprized that the output was: D+ AI was expecting A+ D
有人可以解释吗
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
推荐答案
max
是Python函数,并且不遵守类别排序.它使用基于unicode代码的字典顺序.
max
is a Python function and it does not respect the category ordering. It uses lexicographical ordering based on unicode codes.
如果要考虑分类顺序,则需要使用Series/DataFrames上定义的方法:
If you want to take into account categorical orders, you need to use the methods defined on Series/DataFrames:
print(grades.min(), grades.max())
收益
D A+
这篇关于 pandas 订购了有关考试成绩'D',...,'A +'的分类数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!