数据透视表
数据透视表(Pivot Table)是一种交互式的表,可以进行某些计算,如求和与计数等。 所进行的计算与数据跟数据透视表中的排列有关。 之所以称为数据透视表,是因为可以动态地去改变它们的版面布置,以便按照不同方式分析数据,也可以重新安排行号、列标和页字段 每一次改变版面布置时,数据透视表会立即按照新的布置重新计算数据。 另外,如果原始数据发生更改,则可以更新数据透视表。
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
df = DataFrame( {
'height' : np. random. randint( 155 , 200 , size= 20 ) ,
'size' : np. random. randint( 20 , 50 , size= 20 ) ,
'weight' : np. round ( np. random. uniform( 70 , 120 , size= 20 ) , 1 ) ,
'sex' : np. random. choice( [ '男' , '女' ] , size= 20 ) ,
'smoke' : np. random. choice( [ 'No' , 'Yes' ] , size= 20 )
} )
df
height size weight sex smoke 0 182 35 99.9 女 Yes 1 187 49 72.1 女 Yes 2 192 46 97.9 男 Yes 3 188 32 81.4 男 No 4 159 20 79.4 男 Yes 5 191 42 105.4 女 Yes 6 185 48 116.8 男 Yes 7 161 33 85.1 男 No 8 194 45 97.7 男 No 9 176 36 105.2 男 Yes 10 156 30 94.5 男 No 11 176 43 115.1 女 Yes 12 184 47 119.0 女 Yes 13 167 48 92.2 男 Yes 14 179 34 80.5 男 No 15 185 23 90.0 女 No 16 175 20 70.3 男 Yes 17 190 31 103.5 女 No 18 195 34 91.6 男 No 19 156 22 71.7 女 No
透视表
分组统计的一种方式 如果分组的字段作为索引使用时,使用unstack()和stack()进行堆索引操作
"""
df.pivot_table(
values=None, 期望显示的数据
index=None, 行索引的字段
columns=None, 列索引的字段
aggfunc='mean', 聚合函数,默认是平均值
fill_value=None, 如果遇到NAN值,要进行填充
margins=False, 是否进行合并
dropna=True, 默认删除NAN值
margins_name='All', 合并名称
observed=False,
)
"""
df. pivot_table( index= [ 'sex' ] )
height size weight sex 女 181.375 36.5 97.0875 男 177.250 35.5 91.0500
df. pivot_table( index= [ 'sex' , 'smoke' ] )
height size weight sex smoke 女 No 177.000000 25.333333 88.400000 Yes 184.000000 43.200000 102.300000 男 No 178.833333 34.666667 88.466667 Yes 175.666667 36.333333 93.633333
df. pivot_table( values= [ 'height' , 'weight' ] , index= [ 'sex' , 'smoke' ] )
height weight sex smoke 女 No 177.000000 88.400000 Yes 184.000000 102.300000 男 No 178.833333 88.466667 Yes 175.666667 93.633333
df. pivot_table( index= 'sex' )
height size weight sex 女 181.375 36.5 97.0875 男 177.250 35.5 91.0500
df. pivot_table( index= 'sex' , columns= 'smoke' )
height size weight smoke No Yes No Yes No Yes sex 女 177.000000 184.000000 25.333333 43.200000 88.400000 102.300000 男 178.833333 175.666667 34.666667 36.333333 88.466667 93.633333
df. pivot_table( columns= 'smoke' )
smoke No Yes height 178.222222 179.454545 size 31.555556 39.454545 weight 88.444444 97.572727
df. pivot_table( columns= 'smoke' , aggfunc= 'sum' )
smoke No Yes height 1604.0 1974.0 size 284.0 434.0 weight 796.0 1073.3
df. pivot_table( columns= 'smoke' , aggfunc= 'max' )
smoke No Yes height 195 192 sex 男 男 size 45 49 weight 103.5 119
df. pivot_table( values= [ 'height' , 'size' , 'weight' ] ,
columns= 'smoke' , aggfunc= 'max' )
smoke No Yes height 195.0 192.0 size 45.0 49.0 weight 103.5 119.0
df. pivot_table( values= [ 'height' , 'size' , 'weight' ] ,
columns= 'smoke' , aggfunc= np. sum )
smoke No Yes height 1604.0 1974.0 size 284.0 434.0 weight 796.0 1073.3
def sum_ ( item) :
return item. sum ( )
df. pivot_table( values= [ 'height' , 'size' , 'weight' ] ,
columns= 'smoke' , aggfunc= sum_)
smoke No Yes height 1604.0 1974.0 size 284.0 434.0 weight 796.0 1073.3
def sum_ ( item) :
display( item)
return item. sum ( )
df. pivot_table( values= [ 'height' , 'size' , 'weight' ] ,
columns= 'smoke' , aggfunc= sum_)
3 188
7 161
8 194
10 156
14 179
15 185
17 190
18 195
19 156
Name: height, dtype: int32
0 182
1 187
2 192
4 159
5 191
6 185
9 176
11 176
12 184
13 167
16 175
Name: height, dtype: int32
3 32
7 33
8 45
10 30
14 34
15 23
17 31
18 34
19 22
Name: size, dtype: int32
0 35
1 49
2 46
4 20
5 42
6 48
9 36
11 43
12 47
13 48
16 20
Name: size, dtype: int32
3 81.4
7 85.1
8 97.7
10 94.5
14 80.5
15 90.0
17 103.5
18 91.6
19 71.7
Name: weight, dtype: float64
0 99.9
1 72.1
2 97.9
4 79.4
5 105.4
6 116.8
9 105.2
11 115.1
12 119.0
13 92.2
16 70.3
Name: weight, dtype: float64
smoke No Yes height 1604.0 1974.0 size 284.0 434.0 weight 796.0 1073.3
def sum_ ( item) :
return item. min ( ) if item. name == 'size' else item. mean( )
df. pivot_table( values= [ 'height' , 'size' , 'weight' ] ,
columns= 'smoke' , aggfunc= sum_)
smoke No Yes height 178.222222 179.454545 size 22.000000 20.000000 weight 88.444444 97.572727
交叉表
"""
pd.crosstab(
index,
columns,
values=None,
rownames=None,
colnames=None,
aggfunc=None,
margins=False,
margins_name='All',
dropna=True,
normalize=False,
)
"""
pd. crosstab( df[ 'sex' ] , df[ 'smoke' ] )
pd. crosstab( df[ 'sex' ] , df[ 'smoke' ] , margins= True )
smoke No Yes All sex 女 3 5 8 男 6 6 12 All 9 11 20
pd. crosstab( df[ 'sex' ] , df[ 'smoke' ] , margins= True , margins_name= '小计' )
smoke No Yes 小计 sex 女 3 5 8 男 6 6 12 小计 9 11 20