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')
If I try to pass the categories
df['C']=df['C'].astype('category',categories=['A','B'])
It errors out saying
TypeError: _astype() got an unexpected keyword argument 'categories'
whats the right way to pass categories to 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']))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With