Please help me be more pythonic:
I am label encoding all categorical features with Pandas. I know this can also be done with Sklearn but I'd like to do it with Pandas or Python alone.
I did this by first selecting all columns of type 'obj' which happened to be cat (I am dealing with a small dataframe so I know this for sure). Then, I used a for loop to convert each column.
I know I can definitely do this by avoiding the for loop. The more pythonic the better:
cat_cols = df.select_dtypes(include='object').columns
for col in cat_cols:
df[col] = df[col].astype('category').cat.codes