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?
sessions_df["experience_level"].cat.codes