python科学计算笔记(七)pandas透视表 pivot_table
来源:互联网 发布:用友网络股票分析 编辑:程序博客网 时间:2024/05/19 16:34
The function pandas.pivot_table can be used to create spreadsheet-style pivot tables.
It takes a number of arguments
data: A DataFrame object
values: a column or a list of columns to aggregate
index: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values.
columns: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values.
aggfunc: function to use for aggregation, defaulting to numpy.mean
It takes a number of arguments
data: A DataFrame object
values: a column or a list of columns to aggregate
index: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table index. If an array is passed, it is being used as the same manner as column values.
columns: a column, Grouper, array which has the same length as data, or list of them. Keys to group by on the pivot table column. If an array is passed, it is being used as the same manner as column values.
aggfunc: function to use for aggregation, defaulting to numpy.mean
import numpy as npimport pandas as pdimport datetimedf = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 6, 'B': ['A', 'B', 'C'] * 8, 'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 4, 'D': np.random.randn(24), 'E': np.random.randn(24), 'F': [datetime.datetime(2013, i, 1) for i in range(1, 13)] + [datetime.datetime(2013, i, 15) for i in range(1, 13)]}) pd.pivot_table(df, index=['A', 'B'], columns=['C'], values='D', aggfunc=np.sum)pd.pivot_table(df, index=['C'], columns=['A', 'B'], values='D', aggfunc='sum')pd.pivot_table(df, index=['A', 'B'], columns=['C'], values=['D','E'], aggfunc=np.sum)pd.pivot_table(df, index=['A', 'B'], columns=['C'], values=['D','E'], aggfunc=[np.sum])pd.pivot_table(df, index=['A', 'B'], columns=['C'], values=['D','E'], aggfunc={'D':len,'E':np.sum})pd.pivot_table(df, index=['A', 'B'], columns=['C'], values=['D','E'], aggfunc={'D':len,'E':[np.sum, np.mean]})pd.pivot_table(df, index=pd.Grouper(freq='M', key='F'), columns='C', values='D', aggfunc=np.sum) # 有点类似 resample
阅读全文
0 0
- python科学计算笔记(七)pandas透视表 pivot_table
- pandas学习笔记2—透视表(pivot_table)详解
- Pandas透视表(pivot_table)详解
- Pandas透视表(pivot_table)详解
- Pandas透视表(pivot_table)详解
- 利用python/pandas/numpy做数据分析(三)-透视表pivot_table
- pandas中groupby和pivot_table(数据透视表)
- python科学计算笔记(二)pandas获取网络文件
- python科学计算笔记(五)pandas 时间序列resample
- python科学计算笔记(六)pandas 分组groupby
- python科学计算笔记(十二)pandas的resample采样
- python科学计算一:pandas
- python科学计算四:pandas
- python科学计算笔记(三)pandas中Series和DataFrame练习
- python科学计算笔记(四)pandas 数据索引与选取
- python科学计算笔记(八)pandas大数据HDF5硬盘操作方式
- python科学计算笔记(九)pandas中DataFrame数据操作函数
- python科学计算笔记(十)pandas中时间、日期以及时间序列处理
- Redis 和 I/O 多路复用
- iconv编码转换模块移植到ARM
- Eclipse常用快捷键
- 理解JavaScript中的闭包
- android热修复技术 HotFix
- python科学计算笔记(七)pandas透视表 pivot_table
- 牛顿法
- 要相信:未来是美好的
- 打造完美接口文档
- 信号驱动方式处理UDP数据
- "undefined reference to" 问题解决方法
- C++的运算符重载
- C语言开源项目及c++库大全
- redis+mysql