当前位置: 代码迷 >> 综合 >> pandas DataFrame.pivot_table分类汇总
  详细解决方案

pandas DataFrame.pivot_table分类汇总

热度:55   发布时间:2023-12-08 23:51:22.0

pivot_table(self, values=None, index=None, columns=None, aggfunc=‘mean’, fill_value=None, margins=False, dropna=True, margins_name=‘All’)

import pandas as pd
#创建数据集
df = pd.DataFrame({
    "A": ["foo", "foo", "foo", "foo", "foo","bar", "bar", "bar", "bar"],"B": ["one", "one", "one", "two", "two","one", "one", "two", "two"],"C": ["small", "large", "large", "small","small", "large", "small", "small","large"],"D": [1, 2, 2, 3, 3, 4, 5, 6, 7],"E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
#交叉汇总,按A,B与C的交叉汇总,值为D,计算方式求和
table = pd.pivot_table(df, values='D', index=['A', 'B'],columns=['C'], aggfunc=np.sum)
print (table)#不同列的汇总
table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],aggfunc={
    'D': np.mean,'E': np.average})
print(table)#多种汇总方式
table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],aggfunc={
    'D': np.mean,'E': [min, max, np.mean]})print(table)

在这里插入图片描述

  相关解决方案