# 开篇导包
一、数据聚合 —— df.groupby()详解
DataFrame
参数:
- by: 用作分组的条件对象。(mapping, function, label, or list of labels)
- axis: 轴方向。({0 or ‘index’, 1 or ‘columns’}, default 0)
- level: 索引层级,针对多层索引。(int, level name, or sequence of such, default None)
- as_index: 是否把分组后的组标签作为索引。(bool, default True)
- sort: 是否对分组标签进行排序。(bool, default True)
- group_keys: 调用apply时,是否将组键添加到索引以标识片段。(bool, default True)
- squeeze: 是否对返回值进行维度压缩。(bool, default False)
- observed: 是否显示所有分类值。(bool, default False)
- dropna: 是否删除组键里的NA值。(bool, default True)
返回值:
- DataFrameGroupBy
(一)参数详解
1. by参数
# 导入案例数据
输出:
![94eb2d76fa69f3329cdb6b7a155f6ef9.png](https://img-blog.csdnimg.cn/img_convert/94eb2d76fa69f3329cdb6b7a155f6ef9.png)
1.1 传入单个label
# 传入单个label
输出:
![f80d107f250f9fe05b604c57dfd51cea.png](https://img-blog.csdnimg.cn/img_convert/f80d107f250f9fe05b604c57dfd51cea.png)
1.2 传入多个label
# 传入多个label
输出:
![8ff4414a3b168b5d35ebcfd9623db3d4.png](https://img-blog.csdnimg.cn/img_convert/8ff4414a3b168b5d35ebcfd9623db3d4.png)
1.3 传入一个映射关系mapping
# 传入一个映射关系mapping
输出:
![6bd508beb4f991152483501558ee4088.png](https://img-blog.csdnimg.cn/img_convert/6bd508beb4f991152483501558ee4088.png)
1.4 传入一个function
grp
输出:
![ad4dfa2ff86fd844a8f091d5878f4dd9.png](https://img-blog.csdnimg.cn/img_convert/ad4dfa2ff86fd844a8f091d5878f4dd9.png)
2. axis参数
# 导入案例数据
输出:
![7572a9dbdc6640b0835d207f52d55aba.png](https://img-blog.csdnimg.cn/img_convert/7572a9dbdc6640b0835d207f52d55aba.png)
3. level参数
# 导入案例数据
输出:
![ea5821fa57fdde0a37299cd242d070be.png](https://img-blog.csdnimg.cn/img_convert/ea5821fa57fdde0a37299cd242d070be.png)
4. as_index参数
# 导入案例数据
输出:
![d6df86a9785d3c6daf9def8f781dded1.png](https://img-blog.csdnimg.cn/img_convert/d6df86a9785d3c6daf9def8f781dded1.png)
5. sort参数
# 导入案例数据
输出:
![97c780f919f9a8a737715f251fe394d2.png](https://img-blog.csdnimg.cn/img_convert/97c780f919f9a8a737715f251fe394d2.png)
6. group_keys
group_keys 参数在 apply 操作中很方便,它创建了一个与分组标签[group_keys=True]相对应的额外索引列。
# 导入案例数据
输出:
![ffbb07b4fbbf119fc794bbd59437efa4.png](https://img-blog.csdnimg.cn/img_convert/ffbb07b4fbbf119fc794bbd59437efa4.png)
7. squeeze参数(新版本已经移除,可略过)
df1
输出:
![1f2d7c7204448753a05bb9a2f8c7c0ac.png](https://img-blog.csdnimg.cn/img_convert/1f2d7c7204448753a05bb9a2f8c7c0ac.png)
8. observed参数(新增,没有搞清楚有啥用处,待补充)
# 导入案例数据
输出:
![3450dd9df1cd0e66415cebb4b0318f87.png](https://img-blog.csdnimg.cn/img_convert/3450dd9df1cd0e66415cebb4b0318f87.png)
9. dropna参数
# 导入案例数据
输出:
![bac6a86e429c2610995fc066880af466.png](https://img-blog.csdnimg.cn/img_convert/bac6a86e429c2610995fc066880af466.png)
(二)df.groupby() 与 agg方法的联合使用
# 导入案例数据
输出:
![6607a7be17cf027d836966d5d1ab8d6f.png](https://img-blog.csdnimg.cn/img_convert/6607a7be17cf027d836966d5d1ab8d6f.png)
# 1
输出:
![9bf449b221785ddfd5b01b75da035de5.png](https://img-blog.csdnimg.cn/img_convert/9bf449b221785ddfd5b01b75da035de5.png)
# 2
输出:
![02a0fc1ef25a0a4a369691531cac9a66.png](https://img-blog.csdnimg.cn/img_convert/02a0fc1ef25a0a4a369691531cac9a66.png)
# 3
输出:
![7b66632bbbc29c1b2e726b0853b3c3ea.png](https://img-blog.csdnimg.cn/img_convert/7b66632bbbc29c1b2e726b0853b3c3ea.png)
(三)df.groupby() 与 apply方法的联合使用
# 导入案例数据
输出:
![38d0e988883fcea4779d8fd81a7dd8fa.png](https://img-blog.csdnimg.cn/img_convert/38d0e988883fcea4779d8fd81a7dd8fa.png)
# 1
输出:
![5ff4572eea98fa662efa36aa2bababdb.png](https://img-blog.csdnimg.cn/img_convert/5ff4572eea98fa662efa36aa2bababdb.png)
# 如果传入的函数还有其他参数的话,可以把这些参数放在函数后进行传递
输出:
![fd5931ffc9fa491a1f43ba4c4b6cd177.png](https://img-blog.csdnimg.cn/img_convert/fd5931ffc9fa491a1f43ba4c4b6cd177.png)
(四)df.groupby() 与 pd.cut()方法的联合使用
# 导入案例数据
输出:
![89c1dd8fa23eb7615c8bd7fc4255a2f6.png](https://img-blog.csdnimg.cn/img_convert/89c1dd8fa23eb7615c8bd7fc4255a2f6.png)
# 分箱
输出:
![a90db3f4c0ce0f2fa764ddccbdb9d1d7.png](https://img-blog.csdnimg.cn/img_convert/a90db3f4c0ce0f2fa764ddccbdb9d1d7.png)
示例
pass
二、数据透视
(一)pd.pivot_table()
pandas
参数:
- data: 目标DataFrame
- values: 需要聚合的列名,默认情况下聚合所有数值型的列
- index: 在结果透视表的行上进行分组的列名或者其他分组键 (column, Grouper, array, or list of the previous)
- columns: 在结果透视表的列上进行分组的列名或者其他分组键 (column, Grouper, array, or list of the previous)
- aggfunc: 对数据进行聚合时的函数 (function, list of functions, dict, default numpy.mean)
- fill_value: 用来替换结果表中缺失值的值 (scalar, default None)
- margins: 是否添加行/列小计和总计 (bool, default False)
- dropna: 是否删除所有条目均为NaN的列 (bool, default True)
- margins_name: 小计和总计行/列的名字(str, default ‘All’)
- observed: 是否显示所有分类值 (bool, default False)
返回值:
- DataFrame: An Excel style pivot table.
# 导入案例数据
输出:
![80f4fc5f8ece60acb9ec667d60b3b9ad.png](https://img-blog.csdnimg.cn/img_convert/80f4fc5f8ece60acb9ec667d60b3b9ad.png)
# index and values
输出:
![cbc1739d679cec33909f899af40790fc.png](https://img-blog.csdnimg.cn/img_convert/cbc1739d679cec33909f899af40790fc.png)
# columns
输出:
![baad459b096eebc3f5383481a0bf8814.png](https://img-blog.csdnimg.cn/img_convert/baad459b096eebc3f5383481a0bf8814.png)
# aggfunc
输出:
![61fa851f7d8e8f0518dea6d9f7bf713d.png](https://img-blog.csdnimg.cn/img_convert/61fa851f7d8e8f0518dea6d9f7bf713d.png)
# fill_value
输出:
![a4abe3c8e573799c3e1cc632631f4047.png](https://img-blog.csdnimg.cn/img_convert/a4abe3c8e573799c3e1cc632631f4047.png)
# margins and margin_name
输出:
![117ddf6425a531a40311456c82d0c825.png](https://img-blog.csdnimg.cn/img_convert/117ddf6425a531a40311456c82d0c825.png)
pd.crosstab()
pandas
参数:
- index: 在结果透视表的行上进行分组的列 (array-like, Series, or list of arrays/Series)
- columns: 在结果透视表的列上进行分组的列 (array-like, Series, or list of arrays/Series)
- values: 进行聚合的目标列,需要同时传入aggfunc参数 (array-like, optional)
- rownames: 结果透视表中行索引的名称,需要与index参数中的数量保持一致 (sequence, default None)
- colnames: 结果透视表中列索引的名称,需要与columns参数中的数量保持一致 (sequence, default None)
- aggfunc: 聚合函数 (function, optional)
- margins: 是否添加行/列小计和总计 (bool, default False)
- margins_name: 小计和总计行/列的名字(str, default ‘All’)
- dropna: 是否删除所有条目均为NaN的列 (bool, default True)
- normalize: 是否进行标准化 (bool, {‘all’, ‘index’, ‘columns’}, or {0,1}, default False)
- If passed ‘all’ or True, will normalize over all values.
- If passed ‘index’ will normalize over each row.
- If passed ‘columns’ will normalize over each column.
- If margins is True, will also normalize margin values.
返回值:
- DataFrame: Cross tabulation of the data.
# 导入案例数据
输出:
![edf26c575a2cbc40e6345bb3409317d7.png](https://img-blog.csdnimg.cn/img_convert/edf26c575a2cbc40e6345bb3409317d7.png)
# index、columns、values、aggfunc、rownames、columns、margins、margins_name
输出:
![5ae70316309b5d74255eeea9a08d19ef.png](https://img-blog.csdnimg.cn/img_convert/5ae70316309b5d74255eeea9a08d19ef.png)
# normalize
输出:
![62decf5d261d41bc2f8a61a316ca95b8.png](https://img-blog.csdnimg.cn/img_convert/62decf5d261d41bc2f8a61a316ca95b8.png)