如何使用对应字典重命名 pd.value_counts() 索引

Adr*_*ico 5 python dictionary counting dataframe pandas

我正在做一个value_counts()代表分类值的整数列。

我有一个 dict 将数字映射到与类别名称对应的字符串。

我想找到具有相应名称的索引的最佳方法。因为我对我的 4 行解决方案不满意。

我目前的解决方案

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"}
Run Code Online (Sandbox Code Playgroud)

现在我如何解决问题:

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
Run Code Online (Sandbox Code Playgroud)

我对那个非常乏味的解决方案不满意,你有这种情况的最佳实践吗?

dim*_*ion 7

这是我的解决方案:

>>> weather_correspondance_dict = {1:"sunny", 2:"rainy", 3:"cloudy"}
>>> df["weather"].value_counts().rename(index=weather_correspondance_dict)
    sunny     2
    cloudy    1
    rainy     1
    Name: weather, dtype: int64
Run Code Online (Sandbox Code Playgroud)