在使用 Excel 中,经常会用到数据透视表。所以在 Python 这里,也简单演示下。
1 2 3 4 5
| from pandas import Series,DataFrame import pandas as pd import matplotlib.pyplot as plt %matplotlib inline
|
1 2 3 4 5 6
| tips = sns.load_dataset('tips')
tips.head()
|

数据透视表
1 2 3
| piovt_table = tips.pivot_table(values='tip', index='day') piovt_table
|

1 2 3
| piovt_table = piovt_table.divide(piovt_table.sum(),axis=1) piovt_table
|

数据交叉表
1 2 3
| cross_table = pd.crosstab(index=tips['day'],columns=tips['size']) cross_table
|

1 2 3
| df = cross_table.div(cross_table.sum(1),axis=0) df
|

1 2
| df.plot(kind='bar',stacked=True)
|
