10 Minutes to pandas

By “group by” we are referring to a process involving one or more of the following steps

Splitting the data into groups based on some criteria
Applying a function to each group independently
Combining the results into a data structure
See the Grouping section

代码

df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],
'B': ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],
'C': np.random.randn(8), 'D': np.random.randn(8)})
print(df)
print(df.groupby('A').sum()) # 计算 foo bar 各自对应 C D 列的和(B列无法求和) print(df.groupby(['A','B']).sum()) # 同理,不过这里有个一对多的关系 # A B C D
# 0 foo one 0.102071 -0.301926
# 1 bar one 1.161158 0.847451
# 2 foo two -0.023879 0.936338
# 3 bar three -0.353075 -0.834349
# 4 foo two -0.272542 -1.425635
# 5 bar two -1.016016 -0.031614
# 6 foo one -0.428517 0.892747
# 7 foo three -0.843796 0.614443
# /
# C D
# A
# bar -0.207932 -0.018512
# foo -1.466663 0.715967
# C D
# /
# A B
# bar one 1.161158 0.847451
# three -0.353075 -0.834349
# two -1.016016 -0.031614
# foo one -0.326445 0.590821
# three -0.843796 0.614443
# two -0.296421 -0.489296

Python笔记 #18# Pandas: Grouping的更多相关文章

  1. Python笔记 #15# Pandas: Missing Data

    10 Minutes to pandas import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = ...

  2. Python笔记 #14# Pandas: Selection

    10 Minutes to pandas import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = ...

  3. Python笔记 #13# Pandas: Viewing Data

    感觉很详细:数据分析:pandas 基础 import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = ...

  4. Python笔记 #17# Pandas: Merge

    10 Minutes to pandas Concat df = pd.DataFrame(np.random.randn(10, 4)) print(df) # break it into piec ...

  5. Python笔记 #16# Pandas: Operations

    10 Minutes to pandas #Stats # shift 这玩意儿有啥用??? s = pd.Series([1,5,np.nan], index=dates).shift(0) # s ...

  6. python笔记18(复习)

    今日内容 复习 内容详细 1.Python入门 1.1 环境的搭建 mac系统上搭建python环境. 环境变量的作用:方便在命令行(终端)执行可执行程序,将可执行程序所在的目录添加到环境变量,那么以 ...

  7. 学习笔记之pandas

    Python Data Analysis Library — pandas: Python Data Analysis Library https://pandas.pydata.org/ panda ...

  8. 【Python实战】Pandas:让你像写SQL一样做数据分析(一)

    1. 引言 Pandas是一个开源的Python数据分析库.Pandas把结构化数据分为了三类: Series,1维序列,可视作为没有column名的.只有一个column的DataFrame: Da ...

  9. python笔记 - day8

    python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...

随机推荐

  1. Runtime 应用(一)拦截系统自带的方法交换实现

    动态的交换方法能够给项目中大量已经使用的方法 进行拦截增加操作 实践:利用运行时交换系统的ImageNamed:方法 应用背景 当系统需要适配ios7和ios8时可能会有显示不同图片的需求,但在老项目 ...

  2. 设计模式之抽象工厂模式(Java实现)

    “上次是我的不对,贿赂作者让我先讲来着,不过老婆大人大人有大量,不与我计较,这次还让我先把上次未讲完的应用场景部分给补充上去,有妻如此,夫复何求.”(说完,摸了摸跪的发疼的膝盖,咳咳,我发四我没笑!真 ...

  3. LeetCode——Add Digits

    Description: Given a non-negative integer num, repeatedly add all its digits until the result has on ...

  4. su命令cannot set groups: Operation not permitted的解决方法

    版权声明:本文由曾倩倩原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/103 来源:腾云阁 https://www.qclo ...

  5. Shell主要逻辑源码级分析(1)——SHELL运行流程

    版权声明:本文由李航原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/109 来源:腾云阁 https://www.qclou ...

  6. go练习5--生成md5

    import "crypto/md5" import "encoding/hex" //go 生成 md5 func T4_1() { m := md5.New ...

  7. Python GUI--Tkinter实践

    之前写了Testlink自动执行程序,现使用Tkinter加上GUI试试,想要实现如下图功能 可以实现通过选择要执行的url及报告url自动执行用例,或可以直接写报告结果内容 因项目原因,只列出部分代 ...

  8. idea的svn插件中compare with the same repository version和compare with latest repository version的区别?

    Idea的svn插件中compare with the same repository version和compare with latest repository version的区别? 1.com ...

  9. Oracle Schema Objects——Synonyms

    Oracle Schema Objects 同义词 同义词 = 表的别名. 现在假如说有一张数据表的名称是“USER1.student”,而现在又为这张数据表起了一个“USER1”的名字,以后就可以直 ...

  10. oracle goldengate技术架构-简单试验(全)

    一  GoldenGate简介 Oracle Golden Gate软件是一种基于日志的结构化数据复制备份软件,它通过解析源 数据库在线日志或归档日志获得数据的增量变化,再将这些变化应用到目标数据库, ...