Get a List of Non-Numeric Data Type Columns by Index

Python

Returns a list of data frame columns by index that are object data types. Useful for defining categorical columns when training a Catboost model.

 1|  from pandas.api.types import is_numeric_dtype   
 2|  def get_categorical_indicies(X):  
 3|          cats = []
 4|          for col in X.columns:
 5|              if is_numeric_dtype(X[col]):
 6|                  pass
 7|              else:
 8|                  cats.append(col)
 9|          cat_indicies = []
10|          for col in cats:
11|              cat_indicies.append(X.columns.get_loc(col))
12|          return cat_indicies
13|  categorical_indicies = get_categorical_indicies(X)
Did you find this snippet useful?

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