pandas pivot_table或者groupby实现sql 中的count distinct 功能
pandas pivot_table或者groupby实现sql 中的count distinct 功能
import pandas as pd
import numpy as np
data = pd.read_csv('活跃买家分析初稿.csv')
data.head()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
recycler_key | date 周 | date 年 | date 月 | 记录数 | |
---|---|---|---|---|---|
0 | 1694 | 周 1 | 2018 | 一月 | 6 |
1 | 1693 | 周 1 | 2018 | 一月 | 14 |
2 | 1686 | 周 1 | 2018 | 一月 | 20 |
3 | 1677 | 周 1 | 2018 | 一月 | 62 |
4 | 1676 | 周 1 | 2018 | 一月 | 25 |
- 我们发现表格的表头有空格,且看起来不舒服,尝试使用上篇文章的改名功能,将表头修改为合理的格式
data.columns=['merchant','week','year','month','records']
data.head()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
merchant | week | year | month | records | |
---|---|---|---|---|---|
0 | 1694 | 周 1 | 2018 | 一月 | 6 |
1 | 1693 | 周 1 | 2018 | 一月 | 14 |
2 | 1686 | 周 1 | 2018 | 一月 | 20 |
3 | 1677 | 周 1 | 2018 | 一月 | 62 |
4 | 1676 | 周 1 | 2018 | 一月 | 25 |
- 我们的目标就是统计每个自然月内对应每个客户提交的周次数
- 同样的原理,我们也可以统计自然月内客户数
方法一: 多重groupby,较为麻烦
- 首先利用groupby求出每个月中商家提交订单数
data1 =data.groupby(['month','merchant']).size()
data1.head()
month merchant
一月 1 2
240 1
241 1
256 9
277 2
dtype: int64
- 重建索引
data1.reset_index().head()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
month | merchant | 0 | |
---|---|---|---|
0 | 一月 | 1 | 2 |
1 | 一月 | 240 | 1 |
2 | 一月 | 241 | 1 |
3 | 一月 | 256 | 9 |
4 | 一月 | 277 | 2 |
- 将重建索引的生成的dataFrame再次groupby
data1.reset_index().groupby('month')['merchant'].size().reindex(['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']).reset_index()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
month | merchant | |
---|---|---|
0 | 一月 | 615 |
1 | 二月 | 622 |
2 | 三月 | 359 |
3 | 四月 | 175 |
4 | 五月 | 209 |
5 | 六月 | 258 |
6 | 七月 | 320 |
7 | 八月 | 366 |
8 | 九月 | 417 |
9 | 十月 | 428 |
10 | 十一月 | 522 |
11 | 十二月 | 617 |
方法2 pivot_table使用aggfunc 实现nunique方法
data2=data.pivot_table(index='month',values='merchant',aggfunc=lambda x:len(x.unique()))
data2.reindex(['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']).reset_index()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
month | merchant | |
---|---|---|
0 | 一月 | 615 |
1 | 二月 | 622 |
2 | 三月 | 359 |
3 | 四月 | 175 |
4 | 五月 | 209 |
5 | 六月 | 258 |
6 | 七月 | 320 |
7 | 八月 | 366 |
8 | 九月 | 417 |
9 | 十月 | 428 |
10 | 十一月 | 522 |
11 | 十二月 | 617 |
方法3,直接采用Series的nunique方法
data3 = data.pivot_table(index='month',values='merchant',aggfunc=pd.Series.nunique)
data3.reindex(['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']).reset_index()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
month | merchant | |
---|---|---|
0 | 一月 | 615 |
1 | 二月 | 622 |
2 | 三月 | 359 |
3 | 四月 | 175 |
4 | 五月 | 209 |
5 | 六月 | 258 |
6 | 七月 | 320 |
7 | 八月 | 366 |
8 | 九月 | 417 |
9 | 十月 | 428 |
10 | 十一月 | 522 |
11 | 十二月 | 617 |
方法4 使用单个的groupby,聚合使用nunique方法
data4 = data.groupby(['month']).agg({'merchant': pd.Series.nunique})
data4.reindex(['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']).reset_index()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
month | merchant | |
---|---|---|
0 | 一月 | 615 |
1 | 二月 | 622 |
2 | 三月 | 359 |
3 | 四月 | 175 |
4 | 五月 | 209 |
5 | 六月 | 258 |
6 | 七月 | 320 |
7 | 八月 | 366 |
8 | 九月 | 417 |
9 | 十月 | 428 |
10 | 十一月 | 522 |
11 | 十二月 | 617 |
可以参考
- refer this stackoverflow
pandas pivot_table或者groupby实现sql 中的count distinct 功能的更多相关文章
- VC++2005下的ADO SQL语句(like,count,distinct)和操作(转)
http://blog.sina.com.cn/s/blog_56fd66a70100hxjf.html http://timke.blog.163.com/blog/#m=0 环境:MFC Dia ...
- Python中实现count(distinct )
假设一个表有6个字段c1,c2,c3,c4,c5,c6,有如下的sql语句: select c1,count(distinct(c6)) from tbl where c3>1 group by ...
- 知方可补不足~SQL中的count命令的一些优化措施(百万以上数据明显)
回到目录 SQL中对于求表记录总数的有count这个聚合命令,这个命令给我们感觉就是快,比一般的查询要快,但是,当你的数据表记录比较多时,如百万条,千万条时,对于count来说,就不是那么快了,我们需 ...
- SQL中以count及sum为条件的查询
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
- SQL中以count或sum为条件的查询方式
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
- sql中unique和distinct
在SQL语法里面,有unique和distinct两个关键字, unique是distinct的同义词,功能完全相同.distinct是标准语法,其他数据库 sql server,db2,oracle ...
- sql中简单的触发器功能
触发器分为DML触发器和DDL触发器DML触发器包含After触发器,执行insert update delete语句后会触发after触发器,会事务回滚DML触发器还包含instead of触发器, ...
- Linq 实现普通sql中 where in 的功能
user.ProjectIds 的值是使用逗号分隔的 例如:1,2,3 projectList = (from a in projectList where (user.ProjectIds.Spli ...
- SQL中 select count(1) count中的1 到底是什么意思呢?和count(*)的区别
count(1),其实就是计算一共有多少符合条件的行. 1并不是表示第一个字段,而是表示一个固定值.其实就可以想成表中有这么一个字段,这个字段就是固定值1,count(1),就是计算一共有多少个1.同 ...
随机推荐
- 【数论】Factors of Factorial @upcexam6503
问题 G: Factors of Factorial 时间限制: 1 Sec 内存限制: 128 MB提交: 57 解决: 33[提交][状态][讨论版][命题人:admin] 题目描述 You ...
- pygame 笔记-8 背景音乐&子弹音效
游戏哪能没有音效?这节我们研究下如何加背景音乐,其实也很简单: # 加载背景音乐 pygame.mixer.music.load(music_base_path + "music.mp3&q ...
- Column 'u_id' in field list is ambiguous
- mssql f_Split
mssql可以如下CREATE FUNCTION [dbo].[f_Split] ( @val varchar(max),@Splits varchar(100))RETURNS @Table TAB ...
- 同步IO、异步IO、阻塞IO、非阻塞IO之间的联系与区别
POSIX 同步IO.异步IO.阻塞IO.非阻塞IO,这几个词常见于各种各样的与网络相关的文章之中,往往不同上下文中它们的意思是不一样的,以致于我在很长一段时间对此感到困惑,所以想写一篇文章整理一下. ...
- Git 将代码回到指定版本
将代码回到hash为1fbcb7ea3b43df60c639875d2bb68e20b451059e的版本 git checkout 1fbcb7ea3b43df60c639875d2bb68e20b ...
- .NET 同步与异步 之 线程安全的集合 (十一)
本随笔续接:.NET 同步与异步 之 警惕闭包(十) 无论之前说的锁.原子操作 还是 警惕闭包,都是为安全保驾护航,本篇随笔继续安全方面的主题:线程安全的集合. 先看一下命名空间:System.Col ...
- 【Python】解析Python的缩进规则
Python中的缩进(Indentation)决定了代码的作用域范围.这一点和传统的c/c++有很大的不同(传统的c/c++使用花括号花括号{}符决定作用域的范围:python使用缩进空格来表示作用域 ...
- AspectF写法
AspectF.Define .ProgressBar(caption) .Do(() => { if (!SpecialMenuClick(midForm, tag)) { DockBarSh ...
- 试试SQLServer 2014的内存优化表
SQL Server2014存储引擎:行存储引擎,列存储引擎,内存引擎 SQL Server 2014中的内存引擎(代号为Hekaton)将OLTP提升到了新的高度. 现在,存储引擎已整合进当前的数据 ...