CategoricalIndex.reorder_categories(*args, …):重新排序new_categories中指定的类别。 CategoricalIndex.add_categories(args, *kwargs):添加新类别。 CategoricalIndex.remove_categories(*args, …):删除指定的类别。 CategoricalIndex.remove_unused_categories(…):删除未使用的类别。 CategoricalIndex.set_categories(...
pandas.CategoricalIndex.reorder_categories 原文:pandas.pydata.org/docs/reference/api/pandas.CategoricalIndex.reorder_categories.html CategoricalIndex.reorder_categories(*args, **kwargs) 按照new_categories指定的顺序重新排序类别。 new_categories需要包括所有旧的类别,且不包含新的类别项。 参数: new_categories...
Categories (3, object): ['a', 'b', 'c'] 重排序 使用Categorical.reorder_categories()可以对现有的category进行重排序: In [103]: s = pd.Series([1, 2, 3, 1], dtype="category") In [104]: s = s.cat.reorder_categories([2, 3, 1], ordered=True) In [105]: s Out[105]: 0 1...
Categories (4, object): ['x', 'y', 'z', 4] 使用remove_categories删除category In [80]: s = s.cat.remove_categories([4]) In [81]: s Out[81]: 0 x 1 y 2 z 3 x dtype: category Categories (3, object): ['x', 'y', 'z'] 删除未使用的cagtegory In [82]: s = pd.Ser...
df['new3'] = df['data'].astype('category').cat.reorder_categories(df['data'].unique()).sort_values().values print(df) 运行之后,结果如下图所示: 方法六 后来【月神】还补充了第三个方法,代码如下图所示: import pandas as pd df = pd.DataFrame({ ...
使用categories进行自定义排序。将特定列设置为类型列,并使用CategoricalDtype定义类型顺序。使用astype方法将列转换为自定义类型。之后即可按照此列进行排序。使用set_categories与reorder_categories:set_categories用于设置列的分类,定义新的分类顺序。reorder_categories用于仅更改现有分类的顺序,不改变分类本身...
colorss.astype('category').cat.categories 实际上,对于开始的整数类型映射,我们可以先通过reorder_categories进行重新排序,然后再使用cat.codes来实现对整数的映射,来达到同样的效果。 mapper={'Garsol':0,'Hardon':1,'Bill':2} names= pd.DataFrame(['Garsol','Hardon','Bill'],columns=['name']) ...
通过Categorical.reorder_categories()和Categorical.set_categories()方法可以重新排序类别。对于Categorical.reorder_categories(),所有旧类别必须包含在新类别中,不允许有新类别。这将必然使排序顺序与类别顺序相同。 In [102]: s = pd.Series([ 1, 2, 3, 1], dtype="category")In [103]: s = s.cat.reor...
cat.reorder_categories(['a','c','d'],ordered=True) s >= s 四、问题与练习 4.1. 问题 【问题一】 如何使用union_categoricals方法?它的作用是什么? 如果要组合不一定具有相同类别的类别,union_categoricals函数将组合类似列表的类别。新类别将是合并的类别的并集。如下所示: 代码语言:javascript 代码运行...
cat.add_categories():增加新的分类。 cat.remove_categories():删除指定的分类。 cat.rename_categories():重命名分类。 cat.reorder_categories():按照指定的顺序重新排序分类。 cat.remove_unused_categories():删除未使用的分类。 cat.set_categories():设置分类的新值。