我正在尝试在有序分类dtype的pandas数据框中创建一列(在Jupyter笔记本中),但未排序

时间:2019-11-16 09:11:30

标签: python pandas dataframe categorical-data

我正在尝试基于现有dtype类别的现有列在现有的熊猫数据框中创建有序的类别DType列。我正在运行熊猫0.25.3

我运行了以下代码

print(ri.stop_length.head())
cats = ri['stop_length'].unique()
print(cats)
cattype = CategoricalDtype(categories=cats, ordered=True)

ri['stop_length'] = ri.stop_length.astype('category', cattype)
print(ri.stop_length.cat.ordered)
print(ri.stop_length.head())

这给了我以下输出

stop_datetime
2005-01-04 12:55:00     short
2005-01-23 23:15:00     short
2005-02-17 04:15:00     short
2005-02-20 17:15:00    medium
2005-02-24 01:20:00     short
Name: stop_length, dtype: category
Categories (3, object): [short, medium, long]
[short, medium, long]
Categories (3, object): [short, medium, long]
False
stop_datetime
2005-01-04 12:55:00     short
2005-01-23 23:15:00     short
2005-02-17 04:15:00     short
2005-02-20 17:15:00    medium
2005-02-24 01:20:00     short
Name: stop_length, dtype: category
Categories (3, object): [short, medium, long]

为什么没有得到分类的dtype?

要检查,我已经跑步

ri[ri.stop_length > 'short'].shape

返回以下错误

  

TypeError:无序分类只能比较相等与否

0 个答案:

没有答案