Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting multiple columns to categories in Pandas. apply?

Tags:

python

pandas

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?

like image 948
Amelio Vazquez-Reina Avatar asked Jun 22 '15 23:06

Amelio Vazquez-Reina


1 Answers

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).

like image 110
joelostblom Avatar answered Oct 17 '22 08:10

joelostblom