In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

一、数据透视表生成

首先使用numpy.random函数构造python操作用的基础数据源

In [29]:
# 生成一个100行,5列的数组,并转换为DataFrame格式
# 注意,columns生成的时候尽量不要用list方法,会生成multiindex,
df = pd.DataFrame(np.random.randint(low=10,high=100,size=(100,5)),columns=['a','b','c','d','e'])
df
Out[29]:
a b c d e
0 34 43 94 81 65
1 42 31 51 10 50
2 47 36 49 84 95
3 22 72 11 99 87
4 12 34 54 60 77
... ... ... ... ... ...
95 86 78 77 62 91
96 83 32 13 44 71
97 10 10 84 64 83
98 36 10 75 98 85
99 29 76 84 52 46

100 rows × 5 columns

In [28]:
df.columns
Out[28]:
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
In [35]:
# 对A列数据进行聚合,
table = pd.pivot_table(data=df,index=['a'],values=['b','c'],aggfunc={'b':'count','c':np.sum},margins=True,margins_name='总计',fill_value=0)
table
Out[35]:
b c
a
10 1 84
11 3 257
12 2 79
13 2 162
15 1 59
... ... ...
95 2 92
97 1 21
98 3 154
99 2 143
总计 100 5815

64 rows × 2 columns

透视表的结果本身依然是一个DataFrame格式数据,所以可以使用query()函数来查询

In [37]:
table.query('a ==95')
Out[37]:
b c
a
95 2 92
In [39]:
table.query('a in (10,20)')
Out[39]:
b c
a
10 1 84
20 1 88
In [42]:
table.index
Out[42]:
Index([  10,   11,   12,   13,   15,   16,   17,   18,   19,   20,   22,   24,
         25,   26,   29,   30,   33,   34,   35,   36,   38,   39,   40,   41,
         42,   44,   45,   47,   48,   49,   50,   52,   53,   54,   55,   57,
         58,   59,   61,   62,   64,   65,   66,   71,   72,   74,   75,   76,
         80,   81,   83,   84,   85,   86,   87,   89,   90,   91,   92,   95,
         97,   98,   99, '总计'],
      dtype='object', name='a')
In [46]:
table[table.index=='95']
Out[46]:
b c
a

二、数据透视表修改显示格式

1.数据透视表显示百分比占比

In [51]:
np.random.seed(10)
df1 = pd.DataFrame(np.random.randint(low=10,high=100,size=(100,5)),columns=['a','b','c','d','e'])
df1
Out[51]:
a b c d e
0 19 25 74 38 99
1 39 18 83 10 50
2 46 26 21 64 98
3 72 43 82 88 59
4 61 64 87 79 23
... ... ... ... ... ...
95 16 47 91 80 48
96 12 46 84 66 35
97 92 68 26 96 50
98 48 14 33 68 49
99 10 48 88 83 36

100 rows × 5 columns

In [59]:
dftable = df1.pivot_table(index=['a'],values=['b'],aggfunc='count',margins=True,margins_name='汇总')
# 除以汇总值就是占比
dftable.div(dftable.iloc[-1,-1],axis='columns')
Out[59]:
b
a
10 0.03
11 0.01
12 0.02
14 0.01
16 0.02
... ...
95 0.01
96 0.01
98 0.04
99 0.02
汇总 1.00

61 rows × 1 columns

In [60]:
# 将占比以百分比的形式展示
dftable.div(dftable.iloc[-1,-1],axis='columns').applymap(lambda x: format(x,'.1%'))
Out[60]:
b
a
10 3.0%
11 1.0%
12 2.0%
14 1.0%
16 2.0%
... ...
95 1.0%
96 1.0%
98 4.0%
99 2.0%
汇总 100.0%

61 rows × 1 columns