Groupby value counts on the dataframe pandas

I use groupby and size

df.groupby(['id', 'group', 'term']).size().unstack(fill_value=0)

Timing

1,000,000 rows

df = pd.DataFrame(dict(id=np.random.choice(100, 1000000),
                       group=np.random.choice(20, 1000000),
                       term=np.random.choice(10, 1000000)))

Leave a Comment