我试图在一个有序分类数据类型的pandas数据帧中创建一个列(在Jupyter笔记本中),但它不是有序的

2024-09-30 12:22:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在现有数据帧中,基于现有的DType Categorical列,创建一个有序的DType Categorical列。我正在运行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]

为什么生成的分类数据类型没有排序

为了检查,我已经跑了

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

返回以下错误

TypeError: Unordered Categoricals can only compare equality or not


Tags: objectlengthheadlongcategoriesmediumshortstop

热门问题