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 *