Maybe I'm missing the obvious.
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']
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
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.
You can also call
pd.value_counts
directly on a list.You can generate the appropriate list via
numpy.concatenate
,itertools.chain
, orcytoolz.concat
cytoolz.concat
itertools.chain
numpy.unique
+numpy.concatenate
All yield
time testing
You can normalize the records by stacking them then call
value_counts()
:try this: