pandas :在列表的每个元素上使用groupby [英] Pandas : Use groupby on each element of list
问题描述
也许我错过了显而易见的事情.
Maybe I'm missing the obvious.
我有一个看起来像这样的pandas数据框:
I have a pandas dataframe that looks like this :
id product categories
0 Silmarillion ['Book', 'Fantasy']
1 Headphones ['Electronic', 'Material']
2 Dune ['Book', 'Sci-Fi']
我想使用groupby函数计算类别列中每个元素的出现次数,因此结果将是
I'd like to use the groupby function to count the number of appearances of each element in the categories column, so here the result would be
Book 2
Fantasy 1
Electronic 1
Material 1
Sci-Fi 1
但是,当我尝试使用groupby函数时,pandas会计算整个列表的出现,而不是分离其元素.我尝试过使用元组或拆分的多种不同方式来处理此问题,但到目前为止,我一直没有成功.
However when I try using a groupby function, pandas counts the occurrences of the entire list instead of separating its elements. I have tried multiple different ways of handling this, using tuples or splits, but this far I've been unsuccessful.
推荐答案
您可以通过堆叠记录然后调用value_counts()
来对记录进行规范化:
You can normalize the records by stacking them then call value_counts()
:
pd.DataFrame(df['categories'].tolist()).stack().value_counts()
Out:
Book 2
Fantasy 1
Material 1
Sci-Fi 1
Electronic 1
dtype: int64
这篇关于 pandas :在列表的每个元素上使用groupby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!