What does cat.codes do?


I’m doing the Data Scientist: Natural Language Processing Specialist career path, and I’ve gotten to the point where we’re doing variable types. On the variable types review page, it asks me to use cat.codes, but it never went over this in the lesson. Is anyone willing to explain exactly what cat.codes means and does?

Thank you for the help,
mews_mochi <3

P.S : it also asks me to use this code on the census variables project, not sure how I’m supposed to know that without looking at the hint :sob:

1 Like

Do you have a link to the lesson?

cat.codes assigns a numerical value to the (ordinal) categorical variables. It returns an array of numbers that are paired with the categorical variables (as well as the index). This allows you to do summary statistics on a column of data.
Also, it’s okay to look at the hint if you don’t understand a concept. :slight_smile:

See the Pandas documentation:


And more here on categorical variables:


Example from the Summary Statistics lesson on NYC Tree census data:

health_categories = ['Poor', 'Fair', 'Good']

nyc_trees['health'] = pd.Categorical(nyc_trees['health'], health_categories, ordered=True)

median_index = np.median(nyc_trees['health'].cat.codes)

median_health_status = health_categories[int(median_index)]


Here is a link to the lesson:


Also, thank you for the explanation! Definitely makes more sense now.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.