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.同 ...
随机推荐
- JAVA自学作业01
JAVA自学作业01 1.Hello World 程序 class HelloWorld{ public static void main(String args[]){ System.out.pri ...
- Servlet(2)—java项目下web应用程序
在java项目下手动写一个web程序 步骤: ①创建一个java项目并在根目录创建一个WebContent目录文件 ②WebContent下创建WEB-INF目录文件 ③WEB-INF下创建class ...
- HTML5 学习05—— 拖放(Drag 和 Drop)
拖放(Drag 和 drop)是 HTML5 标准的组成部分.即抓取对象以后拖到另一个位置. 例:将w3cschool图标拖动到矩形框中. <script> function allowD ...
- React组件通信技巧
效果图 communication.gif 点击查看Github完整源码 1.父向子通信 直接标签中插入参数即可 //number只是个例子 let _number = this.state.numb ...
- header 跳转时报错误。Header may not contain more than a single header, new line detected
我在用php的header做跳转时,报错误. Header may not contain more than a single header, new line detected 先贴一下代码: c ...
- 罗技Setpoint控制酷狗等第三方播放器
手里有个淘过来的二手戴尔蓝牙键盘,虽然是戴尔的,但是确实罗技代工的,因此可以使用罗技的Setpoint,用这个软件后可以集中管理罗技的键盘鼠标进行一些个性化设置,如下图所示.不过郁闷的是如果不装Set ...
- angular 2 - 005 路由实现机制
angular2的路由是不是很神奇, url发生了变化却没有看到有任何请求发出? 1. hash模式 url类似 http://localhost:4200/#/task-list,跳转到路由页面再刷 ...
- CSAPP Tiny web server源代码分析及搭建执行
1. Web基础 webclient和server之间的交互使用的是一个基于文本的应用级协议HTTP(超文本传输协议). 一个webclient(即浏览器)打开一个到server的因特网连接,而且请求 ...
- c# 非调试状态下面执行
#if !DEBUG View("ErrorSimple").ExecuteResult(ControllerContext);#endif
- 好用好玩的Python包
ujson 用C++实现的json解析器,速度飞快. prettytable 在控制台下使用表格展示数据 import prettytable t=prettytable.PrettyTable([' ...
