Python 笔记 #18# Pandas: Grouping

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

原文地址:https://www.cnblogs.com/xkxf/p/8394941.html

时间: 2024-10-15 17:20:46

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

Python 笔记 #13# Pandas: Viewing Data

感觉很详细:数据分析:pandas 基础 import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = pd.date_range('20180116', periods=3) # 创建 16 17 18 等六个日期 df = pd.DataFrame(np.random.randn(3,4), index=dates, columns=list('ABCD')) # 这是二维的,类似于一个表! #

Python 笔记 #14# Pandas: Selection

import pandas as pd import numpy as np import matplotlib.pyplot as plt dates = pd.date_range('20180116', periods=3) # 创建 16 17 18 等六个日期 df = pd.DataFrame(np.random.randn(3,4), index=dates, columns=list('ABCD')) # 这是二维的,类似于一个 # Getting # print(df['A']

Python 笔记 #17# Pandas: Merge

10 Minutes to pandas Concat df = pd.DataFrame(np.random.randn(10, 4)) print(df) # break it into pieces pieces = [df[:3], df[3:7], df[7:]] print(pd.concat(pieces)) # 0 1 2 3 # 0 0.879526 -1.417311 -1.309299 0.287933 # 1 -1.194092 1.237536 -0.375177 -0

20180426学习python笔记(pandas使用)

原文地址:https://www.cnblogs.com/beijingjiaotongdaxue/p/8955138.html

Python笔记-Grouping Records Together Based on a Field

Grouping Records Together Based on a Field Problem You have a sequence of dictionaries or instances and you want to iterate over the data in groups based on the value of a particular field, such as date. Solution The itertools.groupby() function is p

python基础教程_学习笔记18:标准库:一些最爱——shelve

标准库:一些最爱 shelve Shelve唯一有趣的函数是open.在调用它的时候(使用文件名作为参数),它会返回一个Shelf对象,可以用它来存储内容.只需要把它当作普通的字典(但是键一定要作为字符串)来操作即可,在完成工作之后,调用它的close方法. 意识到shelve.open函数返回的对象并不是普通的映射是很重要的. >>> import shelve >>> s=shelve.open('a.txt') >>> s['x']=['a','

玩蛇(Python)笔记之基础Part3

玩蛇(Python)笔记之基础Part1 一.集合 1.set 无序,不重复序列 {}创建,直接写元素 2.set功能 __init__()构造方法,,使用强制转换就会调用此方法 1 set1 = {'year', 'jiujiujiu'} 2 print(type(set1)) 3 # 创建集合 4 s = set() # 创建空集合 5 li = [11, 22, 11, 22] 6 s = set(li) set 3.集合的基本操作 1 # 操作集合 2 s1 = set() 3 s1.a

Python笔记(四)

在<Python笔记(三)>中,我记录关于Python中序列问题的知识.个人觉得确实比Java中的集合框架简单.之前也说了,Python是一种高级面向对象的语言,它的每一个变量都称为对象.今天我接触了面向对象的编程.下面是这篇博客的目录: 1.类与对象 2.输入输出 3.异常 类与对象: 我们都知道面向对象的语言具备四个特性:抽象,继承,封装,多态.Java,C++是这样,Python也不例外.在Python中,我们定义一个类,使用关键字class.形式如下:class classname:.

玩蛇(Python)笔记之基础Part2

玩蛇(Python)笔记之基础Part2 一.列表 1.列表 别的语言叫数组 python牛逼非要取个不一样的名字 1 age = 23 2 name = ["biubiubiu", "jiujiujiu", 22, age] 3 # namecopy = name 4 # namecopy.pop() 5 print(name) 6 # print(namecopy) List 2.列表取值 正常index 从零开始,,取倒数加负号 倒数第一就是[-1] 3.列表