Consider a Dataframe. I want to convert a set of columns to_convert
to categories.
I can certainly do the following:
for col in to_convert:
df[col] = df[col].astype('category')
but I was surprised that the following does not return a dataframe:
df[to_convert].apply(lambda x: x.astype('category'), axis=0)
which of course makes the following not work:
df[to_convert] = df[to_convert].apply(lambda x: x.astype('category'), axis=0)
Why does apply
(axis=0
) return a Series even though it is supposed to act on the columns one by one?
Note that since pandas 0.23.0 you no longer apply
to convert multiple columns to categorical data types. Now you can simply do df[to_convert].astype('category')
instead (where to_convert
is a set of columns as defined in the question).
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