透视表

透视表

引入相关库

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

数据获取

df=pd.read_excel('../homework/sales-funnel.xlsx')
df

Acount代表客户账户,Name代表客户名字,Rep代表销售代表名字,Manager代表销售代表老板名字,Product代表卖出的产品,Quantity代表产品质量,Price代表产品价格,Status代表状态

AccountNameRepManagerProductQuantityPriceStatus
0714466Trantow-BarrowsCraig BookerDebra HenleyCPU130000presented
1714466Trantow-BarrowsCraig BookerDebra HenleySoftware110000presented
2714466Trantow-BarrowsCraig BookerDebra HenleyMaintenance25000pending
3737550Fritsch, Russel and AndersonCraig BookerDebra HenleyCPU135000declined
4146832Kiehn-SpinkaDaniel HiltonDebra HenleyCPU265000won
5218895Kulas IncDaniel HiltonDebra HenleyCPU240000pending
6218895Kulas IncDaniel HiltonDebra HenleySoftware110000presented
7412290Jerde-HilpertJohn SmithDebra HenleyMaintenance25000pending
8740150Barton LLCJohn SmithDebra HenleyCPU135000declined
9141962Herman LLCCedric MossFred AndersonCPU265000won
10163416Purdy-KundeCedric MossFred AndersonCPU130000presented
11239344Stokes LLCCedric MossFred AndersonMaintenance15000pending
12239344Stokes LLCCedric MossFred AndersonSoftware110000presented
13307599Kassulke, Ondricka and MetzWendy YuleFred AndersonMaintenance37000won
14688981Keeling LLCWendy YuleFred AndersonCPU5100000won
15729833Koepp LtdWendy YuleFred AndersonCPU265000declined
16729833Koepp LtdWendy YuleFred AndersonMonitor25000presented

生成透视表

在这里插入图片描述
使用pivot_table方法生成透视表,这个函数第一个参数表示我们要对什么数据进行一个透视,index表示要对哪一个columns做一个透视
下面对‘name’这一column做透视表,生成了新的DataFrame,index使用原始表里面‘name’,且经过了去重处理,把同一‘name’的行使用取均值的方法(默认聚合方法)进行了合并(例如Quantiy:(1+1+2/3)=1.3)

pd.pivot_table(df,index=['Name'])
AccountPriceQuantity
Name
Barton LLC740150350001.000000
Fritsch, Russel and Anderson737550350001.000000
Herman LLC141962650002.000000
Jerde-Hilpert41229050002.000000
Kassulke, Ondricka and Metz30759970003.000000
Keeling LLC6889811000005.000000
Kiehn-Spinka146832650002.000000
Koepp Ltd729833350002.000000
Kulas Inc218895250001.500000
Purdy-Kunde163416300001.000000
Stokes LLC23934475001.000000
Trantow-Barrows714466150001.333333

把聚合方法改为sum,Quntity:1+1+2=4

pd.pivot_table(df,index=['Name'],aggfunc='sum')
AccountPriceQuantity
Name
Barton LLC740150350001
Fritsch, Russel and Anderson737550350001
Herman LLC141962650002
Jerde-Hilpert41229050002
Kassulke, Ondricka and Metz30759970003
Keeling LLC6889811000005
Kiehn-Spinka146832650002
Koepp Ltd1459666700004
Kulas Inc437790500003
Purdy-Kunde163416300001
Stokes LLC478688150002
Trantow-Barrows2143398450004

添加两个另外的index,‘Rep’和‘Manager’,新的透视表的index变成了三个

pd.pivot_table(df,index=['Name','Rep','Manager'])
AccountPriceQuantity
NameRepManager
Barton LLCJohn SmithDebra Henley740150350001.000000
Fritsch, Russel and AndersonCraig BookerDebra Henley737550350001.000000
Herman LLCCedric MossFred Anderson141962650002.000000
Jerde-HilpertJohn SmithDebra Henley41229050002.000000
Kassulke, Ondricka and MetzWendy YuleFred Anderson30759970003.000000
Keeling LLCWendy YuleFred Anderson6889811000005.000000
Kiehn-SpinkaDaniel HiltonDebra Henley146832650002.000000
Koepp LtdWendy YuleFred Anderson729833350002.000000
Kulas IncDaniel HiltonDebra Henley218895250001.500000
Purdy-KundeCedric MossFred Anderson163416300001.000000
Stokes LLCCedric MossFred Anderson23934475001.000000
Trantow-BarrowsCraig BookerDebra Henley714466150001.333333

把‘name’去掉,更换‘Rep’和‘Manager’顺序,可以看到一个Manager下面有多个Rep

pd.pivot_table(df,index=['Manager','Rep'])
AccountPriceQuantity
ManagerRep
Debra HenleyCraig Booker720237.020000.0000001.250000
Daniel Hilton194874.038333.3333331.666667
John Smith576220.020000.0000001.500000
Fred AndersonCedric Moss196016.527500.0000001.250000
Wendy Yule614061.544250.0000003.000000

通过values参数来从‘Account Price Quantity‘指定哪个values,例下面只关心Price

pd.pivot_table(df,index=['Manager','Rep'],values=['Price'])
Price
ManagerRep
Debra HenleyCraig Booker20000.000000
Daniel Hilton38333.333333
John Smith20000.000000
Fred AndersonCedric Moss27500.000000
Wendy Yule44250.000000

改变聚合方法变成求和

pd.pivot_table(df,index=['Manager','Rep'],values=['Price'],aggfunc='sum')
Price
ManagerRep
Debra HenleyCraig Booker80000
Daniel Hilton115000
John Smith40000
Fred AndersonCedric Moss110000
Wendy Yule177000

还可以指定多个values

pd.pivot_table(df,index=['Manager','Rep'],values=['Price','Quantity'],aggfunc='sum')
PriceQuantity
ManagerRep
Debra HenleyCraig Booker800005
Daniel Hilton1150005
John Smith400003
Fred AndersonCedric Moss1100005
Wendy Yule17700012

columns方法对于每一个values还可以看不同的columns,例如对于Price和Quantity都可以看product的’CPU Maintenance Monitor Software‘等参数

pd.pivot_table(df,index=['Manager','Rep'],values=['Price','Quantity'],columns=['Product'],aggfunc='sum')
PriceQuantity
ProductCPUMaintenanceMonitorSoftwareCPUMaintenanceMonitorSoftware
ManagerRep
Debra HenleyCraig Booker65000.05000.0NaN10000.02.02.0NaN1.0
Daniel Hilton105000.0NaNNaN10000.04.0NaNNaN1.0
John Smith35000.05000.0NaNNaN1.02.0NaNNaN
Fred AndersonCedric Moss95000.05000.0NaN10000.03.01.0NaN1.0
Wendy Yule165000.07000.05000.0NaN7.03.02.0NaN

fill_value可以同于NaN数值的填充

pd.pivot_table(df,index=['Manager','Rep'],values=['Price','Quantity'],columns=['Product'],fill_value=0,aggfunc='sum')
PriceQuantity
ProductCPUMaintenanceMonitorSoftwareCPUMaintenanceMonitorSoftware
ManagerRep
Debra HenleyCraig Booker6500050000100002201
Daniel Hilton10500000100004001
John Smith350005000001200
Fred AndersonCedric Moss9500050000100003101
Wendy Yule1650007000500007320
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值