Skip to content

Instantly share code, notes, and snippets.

@simonespa
Created June 24, 2023 17:54
Show Gist options
  • Select an option

  • Save simonespa/e109692ace27b1807ff3c8cde7ff08f3 to your computer and use it in GitHub Desktop.

Select an option

Save simonespa/e109692ace27b1807ff3c8cde7ff08f3 to your computer and use it in GitHub Desktop.
Split categorical from numerical features with Pandas
# split numerical variables from categorical
numerical = train.select_dtypes('number')
categorical = train.select_dtypes(exclude='number')
# split high cardinality categorical variables from the low cardinality ones
hc_categorical = categorical[[col for col in categorical.columns if categorical[col].nunique() > 20]]
lc_categorical = categorical[[col for col in categorical.columns if categorical[col].nunique() <= 20]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment