pandas DataFrame.pivot_table classification summary

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)

Insert picture description here

Guess you like

Origin blog.csdn.net/rankiy/article/details/102949096