I have a DataFrame where I want to change several columns from type 'object' to 'category'.
I can change several columns at the same time for float,
dftest[['col3', 'col4', 'col5', 'col6']] = \
dftest[['col3', 'col4', 'col5', 'col6']].astype(float)
For 'category' I can not do it the same, I need to do one by one (or in a loop like here).
for col in ['col1', 'col2']:
dftest[col] = dftest[col].astype('category')
Question: Is there any way of doing the change for all wanted columns at once like in the 'float' example?
If I try to do several columns at the same time I have:
dftest[['col1','col2']] = dftest[['col1','col2']].astype('category')
## NotImplementedError: > 1 ndim Categorical are not supported at this time
My current working test code:
import numpy as np
import pandas as pd
factors= np.array([
['a', 'xx'],
['a', 'xx'],
['ab', 'xx'],
['ab', 'xx'],
['ab', 'yy'],
['cc', 'yy'],
['cc', 'zz'],
['d', 'zz'],
['d', 'zz'],
['g', 'zz']
])
values = np.random.randn(10,4).round(2)
dftest = pd.DataFrame(np.hstack([factors,values]),
columns = ['col1', 'col2', 'col3', 'col4', 'col5', 'col6'])
#dftest[['col1','col2']] = dftest[['col1','col2']].astype('category')
## NotImplementedError: > 1 ndim Categorical are not supported at this time
## it works with individual astype
#dftest['col2'] = dftest['col2'].astype('category')
#dftest['col1'] = dftest['col1'].astype('category')
print(dftest)
## doing a loop
for col in ['col1', 'col2']:
dftest[col] = dftest[col].astype('category')
dftest[['col3', 'col4', 'col5', 'col6']] = \
dftest[['col3', 'col4', 'col5', 'col6']].astype(float)
dftest.dtypes
output:
col1 category
col2 category
col3 float64
col4 float64
col5 float64
col6 float64
dtype: object
== [update] ==
I don't have a problem using the loop now that I know the trick, but I asked the question because I wanted to learn/understand WHY I need to do a loop for 'category' and not for float, if there is no other way of doing it.
.astype('category)
to work for more than 1 column in the future. – PattiepattinDataFrame.astype('category')
is implemented and available – Vive