官方文档
pandas.pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', fill_value=None, margins=False, dropna=True, margins_name='All')
data : 创建透视表的dataframe
values : 要聚合的值, optional
index : 要聚合的index
columns : 要聚合的columns
aggfunc :聚合的方式, default numpy.mean
fill_value : 用来替换透视表的缺失值scalar, default None
margins : 添加所有行,列,例如在后面加个“总和”boolean, default False
dropna : 不要包含条目都是NaN的列boolean, default True
margins_name : 在边距为True时将包含总计的行/列的名称。string, default ‘All’
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]})
>>> df
table1 = pd.pivot_table(df, values='D', index=['A', 'B'],columns=['C'],
... aggfunc=np.sum)
table1
table2 = pd.pivot_table(df, values='D', index=['A', 'B'],
... aggfunc=np.sum)
table2
table3 = pd.pivot_table(df, values='D',columns=['C'],
... aggfunc=np.sum)
table3
table4 = pd.pivot_table(df, values='D', index=['A', 'B'],columns=['C'],
... aggfunc=np.sum,margins=True)
table4
table5 = pd.pivot_table(df, values='D', index=['A', 'B'],columns=['C'],
... aggfunc=np.sum,margins=True,margins_name='total')
table5
df['D'][2:6] = None
table6 = pd.pivot_table(df, values='D', index=['A', 'B'],columns=['C'],
... aggfunc=np.sum)
table6
table6 = pd.pivot_table(df, values='D', index=['A', 'B'],columns=['C'],
... aggfunc=np.sum,fill_value="x")
table6