2

I am trying to convert a string that is a categorical data type into a numeric. I found out that I can use pandas.Categorical, unfortunately, accessing the codes attribute give me an error.

Here is a minimal example of my working code

>>> sessions_df = pd.read_csv("fitness_sessions_2025.csv")
>>> session_df.head()
  user_name sex age experience_level
0     Alice   F  29     Intermediate
1     Alice   F  29     Intermediate
2     Alice   F  29     Intermediate

>>> sessions_df["experience_level"].unique()
array(['Intermediate', 'Beginner', 'Advanced'], dtype=object)

>>> sessions_df["experience_level"] = pd.Categorical(
...     sessions_df["experience_level"],
...     categories=['Beginner', 'Intermediate', 'Advanced'],
...     ordered=True)

>>> sessions_df["experience_level"].codes
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_24656\2056368924.py in <module>
----> 1 sessions_df["experience_level"].codes

~\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py in __getattr__(self, name)
   6202         ):
   6203             return self[name]
-> 6204         return object.__getattribute__(self, name)
   6205 
   6206     @final

AttributeError: 'Series' object has no attribute 'codes'

Can anyone please explain what I am doing wrong and advise the best approach?

1
  • 1
    use following code: sessions_df["experience_level"].cat.codes Commented Oct 30 at 14:58

1 Answer 1

5

You need to use the .cat accessor to access categorical-specific attributes, just like you need to use .dt for datetime-specific attributes, etc.

sessions_df["experience_level"].cat.codes
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.