我正在做一value_counts()列代表分类值的整数。
我有一个字典,将数字映射到与类别名称相对应的字符串。
我想找到具有相应名称的索引的最佳方法。由于我对我的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"}
现在,我如何解决问题:
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
问题
我对那种非常乏味的解决方案不满意,您是否有针对这种情况的最佳实践?
婷婷同学_
牛魔王的故事
摇曳的蔷薇
相关分类