I am doing a value_counts()
over a column of integers that represent categorical values.
I have a dict that maps the numbers to strings that correspond to the category name.
I want to find the best way to have the index with the corresponding name. As I am not happy with my 4 lines solution.
My current solution
df = pd.DataFrame({"weather": [1,2,1,3]})
df
>>>
weather
0 1
1 2
2 1
3 3
weather_correspondance_dict = {1:"sunny", 2:"rainy", 3:"cloudy"}
Now how I solve the problem:
df_vc = df.weather.value_counts()
index = df_vc.index.map(lambda x: weather_correspondance_dict[x] )
df_vc.index = index
df_vc
>>>
sunny 2
cloudy 1
rainy 1
dtype: int64
Question
I am not happy with that solution that is very tedious, do you have a best practice for that situation ?