FAQ: Variable Types - Variable Types Review

This community-built FAQ covers the “Variable Types Review” exercise from the lesson “Variable Types”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Master Statistics with Python

FAQs on the exercise Variable Types Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In the summary, there’s a reference to .cat.codes accessor. Nowhere in the lesson is this covered. It’s also mentioned in the Census Variables exercise. Did a lesson get deleted?

9 Likes

+1 on this
I had the same problem,

2 Likes

Yes! Had the same doubt

1 Like

Same doubt what on earth is that

1 Like

Yes, same problem. I thought I had forgotten but checked my notes and there’s nothing about it.

1 Like

There is no clue on how to use .cat.codes during the leasson. If you follow the 5th hint, you can conclude:

auto['engine_codes'] = auto['engine_size'].cat. Codes print(auto.head())

And you will get the extra column based on the variable engine_size where small = 0, medium = 1 and large =2:

Your codebyte won’t work here b/c it needs more info.

Also, everyone is right; there used to be a discussion in the lesson about using cat.codes.
You might want to report the discrepancy in the lesson under “get Unstuck> Other”.

cat.codes assigns a numerical value to each corresponding categorical item in a series and returns it.

ex:

data = ['green', 'blue', 'purple', 'blue', 'green', 'green', 'orange']
cat_series = pd.Series(data, dtype='category')

print(cat_series)

0     green
1      blue
2    purple
3      blue
4     green
5     green
6    orange
dtype: category
Categories (4, object): ['blue', 'green', 'orange', 'purple']

print(cat_series.cat.codes)

0    1
1    0
2    3
3    0
4    1
5    1
6    2
dtype: int8

Also, this info is in the Pandas docs:

https://pandas.pydata.org/docs/user_guide/categorical.html

2 Likes