pivot_table() crosstab()

1 pivot_table()函数  它根据一个或多个键对数据进行聚合,默认对聚合后的数据求均值

import pandas as pd
import numpy as np
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]})

print(df)
# data     接收DataFrame。表示透视表的数据。无默认。
# values     接收字符串。用于指定想要聚合的数据字段名,默认使用全部数据。默认为None。
# index     接收string或list。表示行分组键。默认为None。
# columns     接收string或list。表示列分组键。默认为None。
# aggfunc     接收functions。表示聚合函数。默认为mean。
# fill_value     接受scalar。表示是否将fill_value的数值代替缺失值。默认为None。
# margins     接收boolearn。表示汇总(Total)功能的开关,设为True后结果集中会出现名为“ALL”的行和列。默认为True。
# dropna     接收boolearn。表示是否删掉全为NaN的列。默认为False。
# margins_name      接收string。表示margins为True时,'All'的名称。
table = df.pivot_table(values='D',        # values默认为数值类型的列,
                       index=['A', 'B'],  # index是行索引
                       columns=['C'],     # columns是列索引,
                       aggfunc=np.sum)    # aggfunc用于指定对聚合后的数据进行操作的函数
print(table)
#      A    B      C  D  E
# 0  foo  one  small  1  2
# 1  foo  one  large  2  4
# 2  foo  one  large  2  5
# 3  foo  two  small  3  5
# 4  foo  two  small  3  6
# 5  bar  one  large  4  6
# 6  bar  one  small  5  8
# 7  bar  two  small  6  9
# 8  bar  two  large  7  9
# C        large  small
# A   B                
# bar one    4.0    5.0
#     two    7.0    6.0
# foo one    4.0    1.0
#     two    NaN    6.0
View Code

2 crosstab()函数     它按照指定的index和columns统计数据帧中出现(index, columns)的频次,即默认统计分组项的频次

import pandas as pd
import numpy as np
df = pd.DataFrame({'类别':['水果','水果','水果','蔬菜','蔬菜','肉类','肉类'],
                   '产地':['美国','中国','中国','中国','新西兰','新西兰','美国'],
                   '水果':['苹果','','草莓','番茄','黄瓜','羊肉','牛肉'],
                   '数量':[5,5,9,3,2,10,8],
                   '价格':[5,5,10,3,3,13,20]})
print(df)
# index     接收string或list。表示行索引键。无默认。
# columns     接收string或list。表示列索引键。无默认。
# values     接收array。表示聚合数据。默认为None。
# aggfunc     接收function。表示聚合函数。默认为None。
# rownames     表示行分组键名。无默认。
# colnames     表示列分组键名。无默认。
# dropna     接收boolearn。表示是否删掉全为NaN的。默认为False。
# margins     接收boolearn。默认为True。汇总(Total)功能的开关,设为True后结果集中会出现名为“ALL”的行和列。
# normalize     接收boolearn。表示是否对值进行标准化。默认为False。
print(pd.crosstab(df['类别'],df['产地'],margins=True))
#    类别   产地  水果  数量  价格
# 0  水果   美国  苹果   5   5
# 1  水果   中国   梨   5   5
# 2  水果   中国  草莓   9  10
# 3  蔬菜   中国  番茄   3   3
# 4  蔬菜  新西兰  黄瓜   2   3
# 5  肉类  新西兰  羊肉  10  13
# 6  肉类   美国  牛肉   8  20
# 产地   中国  新西兰  美国  All
# 类别                   
# 水果    2    0   1    3
# 肉类    0    1   1    2
# 蔬菜    1    1   0    2
# All   3    2   2    7
View Code

参考: https://cloud.tencent.com/developer/article/1437438

         https://blog.csdn.net/aoyuemi8881/article/details/101113106

原文地址:https://www.cnblogs.com/xxswkl/p/12160926.html