python中pandas对多列进行分组统计的实现
作者:光于前裕于后 时间:2022-06-25 03:50:03
使用groupby([ ]).size()统计的结果,值相同的字段值会不显示
如上图所示,第一个空着的行是982499 7 3388 1,因为此行与前面一行的这两个字段值是一样的,所以不显示。第二个空着的行是390192 22 4278 1,因为此行与前面一行的第一个字段值是一样的,所以不显示。这样的展示方式更直观,但对于刚用的人,可能会让其以为是缺失值。
如果还不明白可以看下面的全部数据及操作。
import pandas as pd
res6 = pd.read_csv('test.csv')
res6.shape
(12, 3)
res6.columns
Index(['user_id', 'cate', 'shop_id'], dtype='object')
res6.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 12 entries, 0 to 11
Data columns (total 3 columns):
user_id 12 non-null int64
cate 12 non-null int64
shop_id 12 non-null int64
dtypes: int64(3)
memory usage: 368.0 bytes
res6.describe()
user_id | cate | shop_id | |
---|---|---|---|
count | 1.200000e+01 | 12.000000 | 12.000000 |
mean | 6.468688e+05 | 10.666667 | 3594.000000 |
std | 3.988181e+05 | 6.665151 | 373.271775 |
min | 2.421410e+05 | 7.000000 | 3388.000000 |
25% | 3.901920e+05 | 7.000000 | 3388.000000 |
50% | 4.938730e+05 | 7.000000 | 3388.000000 |
75% | 9.824990e+05 | 10.250000 | 3586.250000 |
max | 1.558165e+06 | 23.000000 | 4278.000000 |
res6
user_id | cate | shop_id | |
---|---|---|---|
0 | 390192 | 20 | 4178 |
1 | 390192 | 23 | 4179 |
2 | 390192 | 22 | 4278 |
3 | 1021819 | 7 | 3388 |
4 | 242141 | 7 | 3388 |
5 | 283284 | 7 | 3388 |
6 | 1558165 | 7 | 3388 |
7 | 533696 | 7 | 3388 |
8 | 982499 | 7 | 3388 |
9 | 493873 | 7 | 3388 |
10 | 493873 | 7 | 3388 |
11 | 982499 | 7 | 3389 |
res6['user_id'].value_counts()
390192 3
982499 2
493873 2
242141 1
1021819 1
533696 1
1558165 1
283284 1
Name: user_id, dtype: int64
res6.groupby(['user_id']).size().sort_values(ascending=False)
user_id
390192 3
982499 2
493873 2
1558165 1
1021819 1
533696 1
283284 1
242141 1
dtype: int64
res6.groupby(['user_id', 'cate']).size().sort_values(ascending=False)
user_id cate
982499 7 2
493873 7 2
1558165 7 1
1021819 7 1
533696 7 1
390192 23 1
22 1
20 1
283284 7 1
242141 7 1
dtype: int64
res6_test = res6.groupby(['user_id', 'cate', 'shop_id']).size().sort_values(ascending=False)
res6_test
user_id cate shop_id
493873 7 3388 2
1558165 7 3388 1
1021819 7 3388 1
982499 7 3389 1
3388 1
533696 7 3388 1
390192 23 4179 1
22 4278 1
20 4178 1
283284 7 3388 1
242141 7 3388 1
dtype: int64
来源:https://drguo.blog.csdn.net/article/details/89670842
标签:pandas,多列,分组统计
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何把IP表存到SQL数据库里去?
2009-11-02 20:21:00
go特性之数组与切片的问题
2023-07-15 18:21:30
Python计算一个点到所有点的欧式距离实现方法
2023-03-29 21:19:11
Python面向对象之多态原理与用法案例分析
2023-08-16 00:09:59
![](https://img.aspxhome.com/file/2023/7/81537_0s.png)
基于Python数据可视化利器Matplotlib,绘图入门篇,Pyplot详解
2022-11-20 07:59:16
![](https://img.aspxhome.com/file/2023/7/99167_0s.jpg)
Python序列化模块之pickle与json详解
2023-07-08 05:48:23
两级联动select刷新后其值保持不变的实现方法
2023-11-05 11:39:33
嵌入式Web视频点播系统实现方法
2007-10-10 21:17:00
导航与搜索合并的可能性
2009-09-27 12:06:00
![](https://img.aspxhome.com/file/UploadPic/20099/27/nav_search-33s.png)
详解如何使用Python编写vim插件
2023-01-08 13:30:39
python实现复制大量文件功能
2023-09-03 16:42:54
Golang断言判断值类型的实现方法
2024-02-16 09:59:38
![](https://img.aspxhome.com/file/2023/3/107343_0s.jpg)
选择一个优秀正文字体的15个技巧
2008-03-20 13:36:00
![](https://img.aspxhome.com/file/UploadPic/20083/20/2008320134124747s.jpg)
有用的SQL语句(删除重复记录,收缩日志)
2008-03-04 16:59:00
Python 打印自己设计的字体的实例讲解
2021-04-24 14:45:21
python一键去抖音视频水印工具
2023-09-26 22:58:21
![](https://img.aspxhome.com/file/2023/3/63173_0s.jpg)
Oracle常见错误代码的分析与解决
2024-01-14 20:28:51
MIME Type 引出的两难困境
2011-06-14 09:45:41
理解 javascript 中的函数表达式与函数声明
2024-04-23 09:08:26
![](https://img.aspxhome.com/file/2023/1/135931_0s.png)
为什么GO不支持循环引用
2024-02-08 15:10:01
![](https://img.aspxhome.com/file/2023/9/113099_0s.png)