Grabing categorical or numerical columns

Hi, just a remark to point out a rudimentary way to grab cat. and num. columns:

D = data.dtypes
categ = [k for k in D.keys() if D[k]=="object"]
numer = [k for k in D.keys() if D[k]!="object"]

seems to work. Indeed, i consider that not(object dtype) is numerical (it can be float64 or int64 in fact)

You will see that scikit-learn provides the make_column_selector helper to do such filtering.
Using pandas you can as well use the method select_dtypes: pandas.DataFrame.select_dtypes — pandas 1.4.2 documentation

It will lead to a bit more readable code.

1 Like