熊猫系列字典中的计数值

FLX

我有一系列这样的大词典:

print(df['genres'])
0                        {'0': '1', '1': '4', '2': '23'}
1             {'0': '1', '1': '25', '2': '4', '3': '37'}
2                                             {'0': '9'}

print(type(df['genres']))
<class 'pandas.core.series.Series'>

print(type(df['genres'][0]))
<class 'dict'>

我想对这些值进行计数以得到如下内容:

{'1': 2, '4': 2, '9': 1, '23': 1, '25': 1, '37': 1}

我尝试了以下方法:

print(Counter(chain.from_iterable(df.genres.values)))
Counter({'0': 3, '1': 2, '2': 2, '3': 1})

print(pd.Series(df['genres']).value_counts())
{'0': '1', '1': '4', '2': '23'}                                  1
{'0': '1', '1': '25', '2': '4', '3': '37'}                       1
{'0': '9'}                                                       1

我认为对于比我更有经验的人来说,这很容易。但是我真的不明白...

广晃

尝试:

pd.DataFrame(list(df.genres)).stack().value_counts().to_dict()

输出:

{'1': 2, '4': 2, '37': 1, '9': 1, '23': 1, '25': 1}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章