Ordinal Categorical Variables - Central Tendency I

In this block of code (for full exercise see link below):

# Convert to categorical type
nyc_trees['health'] = pd.Categorical(
    nyc_trees['health'], health_categories, ordered=True

# Calculate median values
median_index = np.median(nyc_trees['health'].cat.codes)
median_health_status = health_categories[int(median_index)]

In the line where I calculate the median_index, I do not understand how when i call cat .codes, it knows the order of the categories and assigns the right integer to them. Could someone explain that to me?

Thanks in advance :slight_smile:

What lesson is this? is it on the DS path?
What are the results of the code that you wrote?

Did you read up on the pandas method, pd.categorical?

1 Like