2020-04-30
阅读量:
868
dataframe透视聚合,题目详情见正文
df表详情如下:

请计算每个销售人员每种产品的平均销售额,实现效果如下:

参考代码:
np.random.seed(0)
B = pd.Series(["Allen","Lucy","Tom","Alice","Tim","Lily","Allen","Lucy","Tom","Alice","Tim","Lily","Allen","Lucy","Tom","Alice","Tim","Lily","Allen","Lucy","Tom","Alice","Tim","Lily"], dtype="string")
C = pd.Series(np.random.randint(20,40,24, dtype="int"))
D = pd.Series(["产品A","产品A","产品A","产品B","产品B","产品B","产品B","产品B","产品B","产品A","产品A","产品A","产品A","产品A","产品A","产品B","产品B","产品B","产品B","产品B","产品B","产品A","产品A","产品A"])
df = pd.DataFrame({'com':D,'name':B, 'sale':C})
df
pd.pivot_table(df,index = "name",columns="com",values="sale",aggfunc='mean')
代码结果:







评论(0)


暂无数据
推荐帖子
2条评论
6条评论
7条评论