pandas 分类类型不起作用 [英] pandas astype categories not working
本文介绍了 pandas 分类类型不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我厌倦了使用 http://上的文档将列更改为catgeory
pandas.pydata.org/pandas-docs/stable/categorical.html
I tired to change column to catgeory
using documentation from http://pandas.pydata.org/pandas-docs/stable/categorical.html
df = pd.DataFrame({'A':[1,2,3,4,5], 'B':['a','b','c','d','e'], 'C':['A','B','A','B','A']})
df['C']=df['C'].astype('category')
如果我尝试通过类别
df['C']=df['C'].astype('category',categories=['A','B'])
说错了
TypeError: _astype() got an unexpected keyword argument 'categories'
将类别传递给astype()
的正确方法是什么?
whats the right way to pass categories to astype()
?
推荐答案
您现在需要通过CategorialDtype传递它,因为astype
方法不再接受它们
You now need to now pass it in via CategorialDtype as the astype
method no longer accepts them
from pandas.api.types import CategoricalDtype
df = pd.DataFrame({'A':[1,2,3,4,5], 'B':['a','b','c','d','e'], 'C':['A','B','A','B','A']})
df['C']=df['C'].astype(CategoricalDtype(categories=['A','B']))
这篇关于 pandas 分类类型不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文