pythonpandas

Check if pandas dataframe column is Nominal or Ordinal Categorical


In pandas one can easily check if the type of a dataframe column is categorical or not by using the following:

df['column_name'].dtype == 'category'

But how do I know if the categorical data is nominal or ordinal?


Solution

  • A column like 'Age' does not represent nominal or ordinal data, but numerical data. You can find all columns that contain numerical data by using:

    numeric_cols = df._get_numeric_data().columns  # numeric columns
    cols = df.columns  # all columns
    

    Let's say that columns that are not numeric are either nominal or ordinal (for example Gender) than you can find these columns with:

    categorical_cols = list(set(cols) - set(numeric_cols))
    

    If you want to identify nominal versus ordinal data, you would need to define some method for doing so. For example if you have ordinal data for clothing size (small, medium, large) you would have to define a fixed order first.