Elegant way to get all categorical columns in pandas?

Viewed 9929

Is there a way to get all categorical variables in Pandas? The best way I know is to iterate through all columns and check whether the dtype is categorical.

Ultimately, I'd like a one-liner to plot all bar charts of all categorical variables.

2 Answers
catCols = [col for col in df.columns if df[col].dtype=="O"]

catcols is a list will have all the columns from df whose type is O which is object

Related