pandas-10 pd.pivot_table()透视表功能

和excel一样,pandas也有一个透视表的功能,具体demo如下:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame #显示所有列
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)
#设置value的显示长度为100,默认为50
pd.set_option('max_colwidth',100) df = pd.read_excel('./sales-funnel.xlsx')
print(df.head())
'''
Account Name Rep Manager \
0 714466 Trantow-Barrows Craig Booker Debra Henley
1 714466 Trantow-Barrows Craig Booker Debra Henley
2 714466 Trantow-Barrows Craig Booker Debra Henley
3 737550 Fritsch, Russel and Anderson Craig Booker Debra Henley
4 146832 Kiehn-Spinka Daniel Hilton Debra Henley Product Quantity Price Status
0 CPU 1 30000 presented
1 Software 1 10000 presented
2 Maintenance 2 5000 pending
3 CPU 1 35000 declined
4 CPU 2 65000 won
''' print(pd.pivot_table(df, index=['Name']))
'''
Account Price Quantity
Name
Barton LLC 740150 35000 1.000000
Fritsch, Russel and Anderson 737550 35000 1.000000
Herman LLC 141962 65000 2.000000
Jerde-Hilpert 412290 5000 2.000000
Kassulke, Ondricka and Metz 307599 7000 3.000000
Keeling LLC 688981 100000 5.000000
Kiehn-Spinka 146832 65000 2.000000
Koepp Ltd 729833 35000 2.000000
Kulas Inc 218895 25000 1.500000
Purdy-Kunde 163416 30000 1.000000
Stokes LLC 239344 7500 1.000000
Trantow-Barrows 714466 15000 1.333333
对名字进行了去重,将每个人的销售记录取进行统计,上例是求了均值。
这是由aggfunc参数来决定的。
''' print(pd.pivot_table(df, index=['Name'], aggfunc='sum'))
'''
Account Price Quantity
Name
Barton LLC 740150 35000 1
Fritsch, Russel and Anderson 737550 35000 1
Herman LLC 141962 65000 2
Jerde-Hilpert 412290 5000 2
Kassulke, Ondricka and Metz 307599 7000 3
Keeling LLC 688981 100000 5
Kiehn-Spinka 146832 65000 2
Koepp Ltd 1459666 70000 4
Kulas Inc 437790 50000 3
Purdy-Kunde 163416 30000 1
Stokes LLC 478688 15000 2
Trantow-Barrows 2143398 45000 4
''' print(pd.pivot_table(df, index=['Name', 'Rep', 'Manager']))
'''
Account ... Quantity
Name Rep Manager ...
Barton LLC John Smith Debra Henley 740150 ... 1.000000
Fritsch, Russel and Anderson Craig Booker Debra Henley 737550 ... 1.000000
Herman LLC Cedric Moss Fred Anderson 141962 ... 2.000000
Jerde-Hilpert John Smith Debra Henley 412290 ... 2.000000
Kassulke, Ondricka and Metz Wendy Yule Fred Anderson 307599 ... 3.000000
Keeling LLC Wendy Yule Fred Anderson 688981 ... 5.000000
Kiehn-Spinka Daniel Hilton Debra Henley 146832 ... 2.000000
Koepp Ltd Wendy Yule Fred Anderson 729833 ... 2.000000
Kulas Inc Daniel Hilton Debra Henley 218895 ... 1.500000
Purdy-Kunde Cedric Moss Fred Anderson 163416 ... 1.000000
Stokes LLC Cedric Moss Fred Anderson 239344 ... 1.000000
Trantow-Barrows Craig Booker Debra Henley 714466 ... 1.333333
''' print(pd.pivot_table(df, index=['Manager', 'Rep']))
# manager 和 rep 之间 存在 一对多的 关系
'''
Account Price Quantity
Manager Rep
Debra Henley Craig Booker 720237.0 20000.000000 1.250000
Daniel Hilton 194874.0 38333.333333 1.666667
John Smith 576220.0 20000.000000 1.500000
Fred Anderson Cedric Moss 196016.5 27500.000000 1.250000
Wendy Yule 614061.5 44250.000000 3.000000
''' print(pd.pivot_table(df, index=['Manager', 'Rep'], values=['Price', 'Quantity']))
'''
Price Quantity
Manager Rep
Debra Henley Craig Booker 20000.000000 1.250000
Daniel Hilton 38333.333333 1.666667
John Smith 20000.000000 1.500000
Fred Anderson Cedric Moss 27500.000000 1.250000
Wendy Yule 44250.000000 3.000000
''' print(pd.pivot_table(df, index=['Manager', 'Rep'], values=['Price', 'Quantity'], columns=['Product']))
'''
Price ... Quantity
Product CPU Maintenance ... Monitor Software
Manager Rep ...
Debra Henley Craig Booker 32500.0 5000.0 ... NaN 1.0
Daniel Hilton 52500.0 NaN ... NaN 1.0
John Smith 35000.0 5000.0 ... NaN NaN
Fred Anderson Cedric Moss 47500.0 5000.0 ... NaN 1.0
Wendy Yule 82500.0 7000.0 ... 2.0 NaN 由以上输出可以看出,当column指定为product之后,price和quantity进行了细分,将每个product的详情列出。
另外还可以设置一个fill_value的参数,可以将nan填充为某个值。
''' '''
总结:
使用透视表之前,需要对原始数据有一个大概的了解,这样生成的透视表才能够有意义。
'''

pandas-10 pd.pivot_table()透视表功能的更多相关文章

  1. pandas之groupby分组与pivot_table透视表

    zhuanzi: https://blog.csdn.net/qq_33689414/article/details/78973267 pandas之groupby分组与pivot_table透视表 ...

  2. Excel学习 -- 数据透视表功能

    Excel -- 数据透视表基础 数据透视表(Pivot Table)是一种交互式的表,可以进行某些计算,如求和与计数等.所进行的计算与数据跟数据透视表中的排列有关.    之所以称为数据透视表,是因 ...

  3. python pandas使用数据透视表

    1) 官网啰嗦这一堆, pandas.pivot_table函数中包含四个主要的变量,以及一些可选择使用的参数.四个主要的变量分别是数据源data,行索引index,列columns,和数值value ...

  4. 【Excle数据透视表】如何移动数据透视表的位置

    数据透视表创建完成了,现在需要将它移动到D5位置,如何移动呢? 解决办法 通过"移动数据透视表"功能实现数据透视表的位置移动 步骤1 单击数据透视表任意单元格→数据透视表工具→分析 ...

  5. Python中pandas透视表pivot_table功能详解(非常简单易懂)

    一文看懂pandas的透视表pivot_table 一.概述 1.1 什么是透视表? 透视表是一种可以对数据动态排布并且分类汇总的表格格式.或许大多数人都在Excel使用过数据透视表,也体会到它的强大 ...

  6. Pandas透视表(pivot_table)详解

    介绍 也许大多数人都有在Excel中使用数据透视表的经历,其实Pandas也提供了一个类似的功能,名为pivot_table.虽然pivot_table非常有用,但是我发现为了格式化输出我所需要的内容 ...

  7. pandas实现excel中的数据透视表和Vlookup函数功能

    在孩子王实习中做的一个小工作,方便整理数据. 目前这几行代码是实现了一个数据透视表和匹配的功能,但是将做好的结果写入了不同的excel中, 如何实现将结果连续保存到同一个Excel的同一个工作表中?还 ...

  8. 小白学 Python 数据分析(12):Pandas (十一)数据透视表(pivot_table)

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...

  9. pandas常用操作详解(复制别人的)——数据透视表操作:pivot_table()

    原文链接:https://www.cnblogs.com/Yanjy-OnlyOne/p/11195621.html 一文看懂pandas的透视表pivot_table 一.概述 1.1 什么是透视表 ...

随机推荐

  1. Python中如何计算字符串里面某一个字符出现的次数?

    一个突发奇想,想解决一个学习中的行业痛点,让我又再度拾起了Python编程语言的学习.   刚学两天,今天遇到一个题,该题难度不高,但有一点关键点在网上找不到,网上也没有相关的答案,于是我只好千辛万苦 ...

  2. windows获取管理员权限 操作文件

    1.文件右键属性 2.安全tab 一路 “应用”和“确定”

  3. arcpy地理处理工具案例教程-景观形状指数计算

    arcpy地理处理工具案例教程-景观形状指数计算 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 使用方法:输入要素类即可,其余参数均默认. 商务 ...

  4. windows 安装 Composer 报错

    错误信息如下: 解决方法: 在 extension = php_snmp.dll 前加上 ";" 然后重启 php 即可安装

  5. fiddler抓包详解

    image.png 前言 fiddler是一个很好的抓包工具,默认是抓http请求的,对于pc上的https请求,会提示网页不安全,这时候需要在浏览器上安装证书. 一.网页不安全 1.用fiddler ...

  6. Eclipse笔记-sun.misc.BASE64Encoder找不到jar包的解决方法

    从SVN检出新项目,在Eclipse中报错如下: 转: Eclipse笔记-sun.misc.BASE64Encoder找不到jar包的解决方法 2018-01-04 00:36:20 雨临Lewis ...

  7. 【HBase】HBase 单机版安装及使用

    HBase介绍 HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了 ...

  8. matlab学习笔记12_3串联结构体,按属性创建含有元胞数组的结构体,filenames,isfield,isstruct,orderfields

    一起来学matlab-matlab学习笔记12 12_3 结构体 串联结构体,按属性创建含有元胞数组的结构体,filenames,isfield,isstruct,orderfields 觉得有用的话 ...

  9. Laravel 登录后跳转回登录前浏览的页面

    一.经过 Auth 中间件检查后跳转至登录页面 也就是没有通过 auth 中间件的认证检查,被 auth 中间件拦截后跳转至登录页面.这种情况下,Laravel 默认会在用户登录成功后自动跳转回登录前 ...

  10. 记一次偶发的bug排查——redis-py-cluster库的bug

     排查流水账: 通过平台监控,发现很多偶发的查看推荐列表的接口时延大于0.5s 写单元测试,不能重现.在测试环境不能重现.只有在正式环境可以偶发重现. 通过日志埋点,等待重现 不断地加日志埋点后发现耗 ...