pandas groupby与bin计数 [英] Pandas groupby with bin counts

查看:75
本文介绍了 pandas groupby与bin计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的DataFrame:

I have a DataFrame that looks like this:

+----------+---------+-------+
| username | post_id | views |
+----------+---------+-------+
| john     |       1 |     3 |
| john     |       2 |    23 |
| john     |       3 |    44 |
| john     |       4 |    82 |
| jane     |       7 |     5 |
| jane     |       8 |    25 |
| jane     |       9 |    46 |
| jane     |      10 |    56 |
+----------+---------+-------+

,我想将其转换为对属于某些分类的视图的计数,如下所示:

and I would like to transform it to count views that belong to certain bins like this:

+------+------+-------+-------+--------+
|      | 1-10 | 11-25 | 25-50 | 51-100 |
+------+------+-------+-------+--------+
| john |    1 |     1 |     1 |      1 |
| jane |    1 |     1 |     1 |      1 |
+------+------+-------+-------+--------+

我尝试过:

bins = [1, 10, 25, 50, 100]
groups = df.groupby(pd.cut(df.views, bins))
groups.username.count()

但是它仅提供汇总计数,而不提供用户计数.如何获得用户的垃圾箱计数?

But it only gives aggregate counts and not counts by user. How can I get bin counts by user?

(使用我的真实数据)合计计数如下:

The aggregate counts (using my real data) looks like this:

impressions
(2500, 5000]         2332
(5000, 10000]        1118
(10000, 50000]        570
(50000, 10000000]      14
Name: username, dtype: int64

推荐答案

您可以按用户名进行分组,计算分组大小,然后使用unstack():

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

这篇关于 pandas groupby与bin计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆