pandas 透视表 pivot_table

简介:


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

    

复制代码
import numpy as np
import pandas as pd
import datetime

df = 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
复制代码

 



 本文转自罗兵博客园博客,原文链接:http://www.cnblogs.com/hhh5460/p/5597314.html,如需转载请自行联系原作者

相关文章
|
1月前
|
索引 Python
如何使用Python的Pandas库进行数据透视表(pivot table)操作?
如何使用Python的Pandas库进行数据透视表(pivot table)操作?
16 0
|
9月前
|
JSON 数据可视化 数据挖掘
python数据可视化开发(2):pandas读取Excel的数据格式处理(数据读取、指定列数据、DataFrame转json、数学运算、透视表运算输出)
python数据可视化开发(2):pandas读取Excel的数据格式处理(数据读取、指定列数据、DataFrame转json、数学运算、透视表运算输出)
217 0
|
4月前
|
数据可视化 数据挖掘 Linux
【数据分析与可视化】Pandas可视化与数据透视表的讲解及实战(超详细 附源码)
【数据分析与可视化】Pandas可视化与数据透视表的讲解及实战(超详细 附源码)
43 0
|
6月前
|
数据挖掘 索引 Python
【100天精通Python】Day60:Python 数据分析_Pandas高级功能-数据透视表pivot_table()和数据交叉表crosstab()常用功能和操作
【100天精通Python】Day60:Python 数据分析_Pandas高级功能-数据透视表pivot_table()和数据交叉表crosstab()常用功能和操作
107 0
|
6月前
|
数据挖掘 索引 Python
【100天精通Python】Day57:Python 数据分析_Pandas数据描述性统计,分组聚合,数据透视表和相关性分析
【100天精通Python】Day57:Python 数据分析_Pandas数据描述性统计,分组聚合,数据透视表和相关性分析
51 0
|
7月前
|
数据挖掘 索引 Python
pandas数据分析之数据重塑透视(stack、unstack、melt、pivot)
在数据分析的过程中,分析师常常希望通过多个维度多种方式来观察分析数据,重塑和透视是常用的手段。 数据的重塑简单说就是对原数据进行变形,为什么需要变形,因为当前数据的展示形式不是我们期望的维度,也可以说索引不符合我们的需求。对数据的重塑不是仅改变形状那么简单,在变形过程中,数据的内在数据意义不能变化,但数据的提示逻辑则发生了重大的改变。 数据透视是最常用的数据汇总工具,Excel 中经常会做数据透视,它可以根据一个或者多个指定的维度来聚合数据。pandas 也提供了数据透视函数来实现这些功能。 如果能熟练区分和使用各种重塑和透视分析方法,那用pandas处理分析日常的数据基本上就没有什么难度了。
125 0
|
28天前
|
数据格式 Python
如何使用Python的Pandas库进行数据透视图(melt/cast)操作?
Pandas的`melt()`和`pivot()`函数用于数据透视。基本步骤:导入pandas,创建DataFrame,然后使用这两个函数转换数据格式。示例代码展示了如何通过`melt()`转为长格式,再用`pivot()`恢复为宽格式。输入数据是包含'Name'和'Age'列的DataFrame,最终结果经过转换后呈现出不同的布局。
39 6
|
28天前
|
数据挖掘 数据处理 索引
如何使用Python的Pandas库进行数据筛选和过滤?
Pandas是Python数据分析的核心库,其DataFrame数据结构便于数据操作。筛选与过滤数据主要包括:导入pandas,创建DataFrame,通过布尔索引、`query()`或`loc[]`、`iloc[]`方法筛选。
|
29天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名?
Pandas在Python中提供数据排序和排名功能。使用`sort_values()`进行排序,如`df.sort_values(by='A', ascending=False)`进行降序排序;用`rank()`进行排名,如`df['A'].rank(ascending=False)`进行降序排名。多列操作可传入列名列表,如`df.sort_values(by=['A', 'B'], ascending=[True, False])`。
23 6
|
30天前
|
索引 Python
如何在Python中使用Pandas库进行季节性调整?
Pandas库在Python中支持季节性调整,通过`seasonal_decompose`函数实现。步骤包括:导入Pandas和statsmodels模块,准备时间序列DataFrame,调用函数分解数据为趋势、季节性和残差,可选地分析或绘制这些部分,以及根据需求去除季节性影响(原始数据减去季节性成分)。这是基础的季节性调整流程,可按实际需求调整。
46 0