在对数据进行分组之后,可以对分组后的数据进行聚合处理统计。

agg函数,agg的形参是一个函数会对分组后每列都应用这个函数。

import pandas as pd
import numpy as np
idx = [101,101,101,102,102,102,103,103,103]
idx += [101,102,103]
name = ["apple","pearl","orange", "apple","pearl","orange","apple","pearl","orange"]
name += ["apple"] * 3
price = [1.0,2.0,3.0,4.00,5.0,6.0,7.0,8.0,9.0]
price += [4] * 3
df0 = pd.DataFrame({ "fruit": name, "price" : price, "supplier" :idx})
print "*" * 30
print df0
print "*" * 30
dg1 = df0.groupby(["fruit", "supplier"])
for n, g in dg1:
print "multiGroup on:", n, "\n|",g ,"|"
print "*" * 30
print dg1.agg(np.mean)

程序的执行结果:

******************************
fruit price supplier
0 apple 1 101
1 pearl 2 101
2 orange 3 101
3 apple 4 102
4 pearl 5 102
5 orange 6 102
6 apple 7 103
7 pearl 8 103
8 orange 9 103
9 apple 4 101
10 apple 4 102
11 apple 4 103
******************************
multiGroup on: ('apple', 101)
| fruit price supplier
0 apple 1 101
9 apple 4 101 |
...
multiGroup on: ('pearl', 103)
| fruit price supplier
7 pearl 8 103 |
******************************
price
fruit supplier
apple 101 2.5
102 4.0
103 5.5
orange 101 3.0
102 6.0
103 9.0
pearl 101 2.0
102 5.0
103 8.0

请注意水果apple的输出。

  • agg应用均值、求和、最大等示例。

    import pandas as pd
    import numpy as np
    idx = [101,101,101,102,102,102,103,103,103]
    idx += [101,102,103] * 3
    name = ["apple","pearl","orange", "apple","pearl","orange","apple","pearl","orange"]
    name += ["apple"] * 3 + ["pearl"] * 3 + ["orange"] * 3
    price = [4.1,5.3,6.3,4.20,5.4,6.0,4.5,5.5,6.8]
    price += [4] * 3 + [5] * 3 + [6] * 3
    df0 = pd.DataFrame({ "fruit": name, "price" : price, "supplier" :idx})
    print "*" * 30
    print df0
    print "*" * 30
    dg1 = df0.groupby(["fruit", "supplier"])
    print dg1.agg(np.mean)
    print "*" * 30
    print dg1.agg([np.mean, np.std, np.min, np.sum])
  • 程序执行结果:

    ******************************
    fruit price supplier
    0 apple 4.1 101
    ...
    17 orange 6.0 103
    ******************************
    price
    fruit supplier
    apple 101 4.05
    102 4.10
    103 4.25
    orange 101 6.15
    102 6.00
    103 6.40
    pearl 101 5.15
    102 5.20
    103 5.25
    ******************************
    price
    mean std amin sum
    fruit supplier
    apple 101 4.05 0.070711 4 8.1
    102 4.10 0.141421 4 8.2
    103 4.25 0.353553 4 8.5
    orange 101 6.15 0.212132 6 12.3
    102 6.00 0.000000 6 12.0
    103 6.40 0.565685 6 12.8
    pearl 101 5.15 0.212132 5 10.3
    102 5.20 0.282843 5 10.4
    103 5.25 0.353553 5 10.5

     

各列用不同的处理函数。需要在agg函数里以字典的形式给出,分组后的那列用那个函数处理。

import pandas as pd
import numpy as np
idx = [101,101,101,102,102,102,103,103,103]
idx += [101,102,103] * 3
name = ["apple","pearl","orange", "apple","pearl","orange","apple","pearl","orange"]
name += ["apple"] * 3 + ["pearl"] * 3 + ["orange"] * 3
price = [4.1,5.3,6.3,4.20,5.4,6.0,4.5,5.5,6.8]
price += [4] * 3 + [5] * 3 + [6] * 3
df0 = pd.DataFrame({ "fruit": name, "price" : price, "supplier" :idx})
print "*" * 30
print df0
print "*" * 30
dg1 = df0.groupby(["fruit"])
print dg1.agg(np.mean)
print "*" * 30
print dg1.agg([np.mean, np.std, np.min, np.sum])
print "*" * 30
print dg1.agg({"price" : np.mean, "supplier" : np.max})

程序的执行结果:

******************************
fruit price supplier
0 apple 4.1 101
1 pearl 5.3 101
2 orange 6.3 101
3 apple 4.2 102
4 pearl 5.4 102
5 orange 6.0 102
6 apple 4.5 103
7 pearl 5.5 103
8 orange 6.8 103
9 apple 4.0 101
10 apple 4.0 102
11 apple 4.0 103
12 pearl 5.0 101
13 pearl 5.0 102
14 pearl 5.0 103
15 orange 6.0 101
16 orange 6.0 102
17 orange 6.0 103
******************************
price supplier
fruit
apple 4.133333 102
orange 6.183333 102
pearl 5.200000 102
******************************
price supplier
mean std amin sum mean std amin sum
fruit
apple 4.133333 0.196638 4 24.8 102 0.894427 101 612
orange 6.183333 0.325064 6 37.1 102 0.894427 101 612
pearl 5.200000 0.228035 5 31.2 102 0.894427 101 612
******************************
supplier price
fruit
apple 103 4.133333
orange 103 6.183333
pearl 103 5.200000

agg函数是对列而言的,如果打算对分组后列的数据进行处理可以使用tranform函数,见下一章。

Pandas的数据分组-aggregate聚合的更多相关文章

  1. pandas学习(数据分组与分组运算、离散化处理、数据合并)

    pandas学习(数据分组与分组运算.离散化处理.数据合并) 目录 数据分组与分组运算 离散化处理 数据合并 数据分组与分组运算 GroupBy技术:实现数据的分组,和分组运算,作用类似于数据透视表 ...

  2. 小白学 Python 数据分析(11):Pandas (十)数据分组

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...

  3. pandas中数据聚合【重点】

    数据聚合 数据聚合是数据处理的最后一步,通常是要使每一个数组生成一个单一的数值. 数据分类处理: 分组:先把数据分为几组 用函数处理:为不同组的数据应用不同的函数以转换数据 合并:把不同组得到的结果合 ...

  4. pandas分组和聚合

    Pandas分组与聚合 分组 (groupby) 对数据集进行分组,然后对每组进行统计分析 SQL能够对数据进行过滤,分组聚合 pandas能利用groupby进行更加复杂的分组运算 分组运算过程:s ...

  5. 利用Python进行数据分析-Pandas(第五部分-数据规整:聚合、合并和重塑)

    在许多应用中,数据可能分散在许多文件或数据库中,存储的形式也不利于分析.本部分关注可以聚合.合并.重塑数据的方法. 1.层次化索引 层次化索引(hierarchical indexing)是panda ...

  6. MySQL聚合函数与数据分组

    我们最常需要的是汇总数据而不是把他们实际检索出来 确定表中行数(或满足某个条件或包含某个特定值的行数) 确定表中行组的和 找出表列(或所有行或特定列)的最大值,最小值和平均值 聚集函数是运行在行组上, ...

  7. python中pandas数据分析基础3(数据索引、数据分组与分组运算、数据离散化、数据合并)

    //2019.07.19/20 python中pandas数据分析基础(数据重塑与轴向转化.数据分组与分组运算.离散化处理.多数据文件合并操作) 3.1 数据重塑与轴向转换1.层次化索引使得一个轴上拥 ...

  8. pandas中的分组技术

    目录 1  分组操作 1.1  按照列进行分组 1.2  按照字典进行分组 1.3  根据函数进行分组 1.4  按照list组合 1.5  按照索引级别进行分组 2  分组运算 2.1  agg 2 ...

  9. MongoDB学习(使用分组、聚合和映射-归并)

    使用分组.聚合和映射-归并 MongoDB的强大功能之一,是直接在服务器对文档的值进行复杂的操作,而不用先发文档发送到客户端在进行处理. 结果分组 对大型数据集进行查询操作时,通常会根据文档的字段值对 ...

随机推荐

  1. Eclipse的基本设置与使用

    下载完eclipse后,还不能立即来写代码,需要完成一些必要的设置 设置 1.对整个工作区设置编码格式 选择菜单栏中的"Window"选项,然后选择"Preference ...

  2. IDEA 2020.3 更新了,机器学习都整上了

    Hello,大家好,我是楼下小黑哥~ 上周 Java 开发申请神器 IDEA 2020.3 新版正式发布: 小黑哥第一时间就在开发机上更新了新版本,并且完整体验了两周了. 下面介绍一下这个版本的主要功 ...

  3. Kafka客户端编程入门介绍

    1.maven依赖 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka ...

  4. MybatisPlus_01

    目录 1.1 简介 1.1.1 特性 1.1.2 框架结构 2.1 快速开始 2.1.1 2.1.2 yaml文件配置 2.1.3 编码 2.1.4 测试 3.1 思考 1.1 简介 MyBatis- ...

  5. Numpy的学习6-深浅赋值(copy&deep copy)

    # = 的赋值方式会带有关联性 import numpy as np a = np.arange(4) # array([0, 1, 2, 3]) b = a c = a d = b # 改变a的第一 ...

  6. 使用docker-maven-plugin打包

    今天在部署的时候遇到点问题,总结一下,docker部署的步骤,如果对您有帮助,关注一下,就是对我最大的肯定, 谢谢! 微服务部署有两种方法: (1)手动部署:首先基于源码打包生成jar包(或war包) ...

  7. Java详细指南

    Java 基础 并发 JVM Java8 新特性 代码优化 网络 操作系统 数据结构与算法 数据库 系统设计 设计模式 常用框架 网站架构 软件底层 其他 Java 基础 <Head First ...

  8. (第一篇)记一次python分布式web开发(利用docker)

    作者:落阳 日期:2020-12-23 在一次项目开发中,决定使用docker+nginx+flask+mysql的技术栈来开发,用此系列文章记录开发的过程. 系列文章,当前为第一篇,记录一次pyth ...

  9. 移位运算符<<与>>

    程序设计中,我们有时会看到两种运算符:<<和>>,这两种运算符均为移位运算符,属于位操作运算符中的一种,分别为<<(左移)和>>(右移). 其中,左移运 ...

  10. 【Azure App Service】C#下制作的网站,所有网页本地测试运行无误,发布至Azure之后,包含CHART(图表)的网页打开报错,错误消息为 Runtime Error: Server Error in '/' Application

    问题描述 C#下制作的网站,所有网页本地测试运行无误,发布至Azure之后,包含CHART(图表)的网页打开报错,错误消息为 Runtime Error: Server Error in '/' Ap ...