我正试图在现有数据帧中,基于现有的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
目前没有回答
相关问题 更多 >
编程相关推荐