Convert All Non-numeric Columns to Category Data Types


Converts all object data types in a dataframe to category data types. Useful for when training models such as Catboost where categorical fields need to be provided as category data types.

 1|  from pandas.api.types import is_numeric_dtype  
 2|  def convert_cats(df):
 3|          cats = []
 4|          for col in df.columns:
 5|              if is_numeric_dtype(df[col]):
 6|                  pass
 7|              else:
 8|                  cats.append(col)
 9|          for col in cats:
10|              df[col] = df[col].astype('category')
11|  convert_cats(X)
Did you find this snippet useful?

Sign up for free to to add this to your code library