Pandas groupby with bin counts

You could group by both the bins and username, compute the group sizes and then use unstack():

>>> groups = df.groupby(['username', pd.cut(df.views, bins)])
>>> groups.size().unstack()
views     (1, 10]  (10, 25]  (25, 50]  (50, 100]
username
jane            1         1         1          1
john            1         1         1          1

Leave a Comment