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. img标签-srcset属性

    今天看前辈的代码时,发现img标签有个陌生的srcset属性,如下: 1 <img class="Avatar" src="https://pic3.zhimg.c ...

  2. JavaScript实现本地图片上传预览功能(兼容IE、chrome、FF)

    需要解决的问题有:本地图片如何在上传前预览.编辑:最近发现这个功能很多是基于flash实现的,很多JavaScript实现的代码兼容性都很差,特别是在IE和firefox和chrome三个浏览器上不兼 ...

  3. java基础---->Java关于复制的使用(一)

    这里简单记录一下java中关于浅复制和深复制的知识.很多时候,一个人选择了行走,不是因为欲望,也并非诱惑,他仅仅是听到了自己内心的声音. java中的复制clone方法 一.java对象的浅复制 一个 ...

  4. LeetCode——Peeking Iterator

    Description: Given an Iterator class interface with methods: next() and hasNext(), design and implem ...

  5. LeetCode——Largest Number

    Description: Given a list of non negative integers, arrange them such that they form the largest num ...

  6. fabric入门

    author: headsen  chen date: 2018-08-12  23:13:16 1,安装 yum -y install epel-releaseyum -y install fabr ...

  7. applicationContext.xml的文件位置就可以有两种默认实现

    ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...

  8. onethink文章详情如何做上一篇和下一篇!

    其实很简单,如果要做上一篇和下一篇,只要知道当前文章ID的前一个ID和后一个ID即可: //上一篇文章 $prewhere = array(); $prewhere['id'] = array('LT ...

  9. stm32的VCC/VDD/VSS/VEE/VBAT的区别

    先看一下stm32vet6的引脚图吧 电路设计以及PCB制作中,经常碰见电源符号:VCC. VDD.VEE.VSS,他们具有什么样的关系那? 一.解释 VCC:C=circuit 表示电路的意思, 即 ...

  10. Spring----学习参考博客书单链接

    [References] 1.IOC之基于Java类的配置Bean 2.IOC之基于注解的配置bean(上) 3.Spring之IOC的注入方式总结 4.Spring之IOC自动装配解析 5.Spri ...