Reputation: 452
Hi I have a list column in my data-frame and would like group by values in the list. Below is the piece of code and expected output
import pandas as pd
df=pd.DataFrame([ { "id" : "1", "fix": ["fix1", "fix2"] }, { "id" : "2", "fix": ["fix2", "fix3"] } ])
res=df.groupby(["fix"]).count() /* this does not work */
Output
fix1 - 1
fix2- 2
fix3 -1
Upvotes: 0
Views: 18
Reputation: 862661
Use DataFrame.explode
first:
res=df.explode('fix').groupby(["fix"]).count()
print (res)
fix
fix1 1
fix2 2
fix3 1
Similar solution with Series.explode
and Series.value_counts
:
res=df['fix'].explode().value_counts()
print (res)
fix2 2
fix3 1
fix1 1
Name: fix, dtype: int64
Upvotes: 2