一文看懂pandas中的透视表
读取数据
import pandas as pd
import numpy as np
df = pd.read_excel("./sales-funnel.xlsx") # 当前目录下的文件
df.head()
image
设置数据
使用category
数据类型,按照想要查看的方式设置顺序
不严格要求,但是设置了顺序有助于分析,一直保持所想要的顺序
df["Status"] = df["Status"].astype("category")
df["Status"].cat.set_categories(["won","pending","presented","declined"],inplace=True) # 设置顺序
建立透视表
- 只使用index参数
pd.pivot_table(df,index=["Manager","Rep"]) # index表示索引
image
- 使用index和values两个参数
image
- 使用aggfunc参数,指定多个函数
image
4.使用columns参数,指定生成的列属性
image
- 解决数据的NaN值,使用fill_value参数
image
- 查看总数据,使用margins=True
image
- 不同的属性字段执行不同的函数
image
image-20200426093414531
- Status排序作用的体现
image-20200426094333109
高级功能
当通过透视表生成了数据之后,便被保存在了数据帧中
查询指定的字段值的信息
image-20200426093516959
图形备忘录
image