我正在研究一个场景,我必须找到在 python 中创建的 Picot 表的小计和总计。收到错误并请求您提供反馈。
A B C D E F G H %I J K
W P E 6 0 0 0 0 0% 0 0
W P F 3 0 0 0 0 0% 0 0
W P G 1 0 0 0 0 0% 0 0
X P H 23 2 2 0 0 76% 0 0
X P I 8 0 0 0 0 0% 0 0
X P J 1 0 0 0 0 0% 0 0
X P E 5 2 0 1 0 78% 0 0
X P K 36 0 0 0 0 0% 0 0
X P F 35 5 5 0 0 89% 1 0
X P G 40 9 6 2 1 22% 4 0
X P L 6 1 0 1 0 45% 0 0
A、B 和 C 列是索引列,我正在尝试获取
tbl = pd.pivot_table(
C,
values=['D', 'E', 'F', 'G', 'H', '% I', 'J', 'K'],
index=['A', 'B', 'C'],
aggfunc=[{
'D': np.sum,
'E': np.sum,
'F': np.sum,
'G': np.sum,
'H': np.sum,
'% I': np.sum,
'J': np.sum,
'K': np.sum}],
fill_value=0,
margins=True,
margins_name='Total')
预期产出
A B C D E F G H %I J K
W P E 6 0 0 0 0 0% 0 0
W P F 3 0 0 0 0 0% 0 0
W P G 1 0 0 0 0 0% 0 0
Sub Total (W) 10 0 0 0 0 Auto% 0 0
X P H 23 2 2 0 0 76% 0 0
X P I 8 0 0 0 0 0% 0 0
X P J 1 0 0 0 0 0% 0 0
X P E 5 2 0 1 0 78% 0 0
X P K 36 0 0 0 0 0% 0 0
X P F 35 5 5 0 0 89% 1 0
X P G 40 9 6 2 1 22% 4 0
X P L 6 1 0 1 0 45% 0 0
Sub Total (X) 154 19 13 4 1 Auto% 5 0
Grand Total 164 19 13 4 1 Auto% 5 0
假设我正确理解您的问题,您必须手动计算特定列的小计和总计。由于我不知道您想要哪个聚合函数,因此我假设它是平均值,但您可以轻松更改它。只需尝试一下代码即可实现您想要的效果。所以,像这样的东西可以完成这项工作:
import pandas as pd
import numpy as np
data = {
'A': ['W', 'W', 'W', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'],
'B': ['P'] * 11,
'C': ['E', 'F', 'G', 'H', 'I', 'J', 'E', 'K', 'F', 'G', 'L'],
'D': [6, 3, 1, 23, 8, 1, 5, 36, 35, 40, 6],
'E': [0, 0, 0, 2, 0, 0, 2, 0, 5, 9, 1],
'F': [0, 0, 0, 2, 0, 0, 0, 0, 5, 6, 0],
'G': [0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 1],
'H': [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
'% I': ['0%', '0%', '0%', '76%', '0%', '0%', '78%', '0%', '89%', '22%', '45%'],
'J': [0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0],
'K': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}
df = pd.DataFrame(data)
pivot_table = pd.pivot_table(
df,
values=['D', 'E', 'F', 'G', 'H', 'J', 'K'],
index=['A', 'B', 'C'],
aggfunc=np.sum,
fill_value=0,
margins=True,
margins_name='Total'
)
df['% I'] = df['% I'].str.rstrip('%').astype(float)
pivot_table['% I'] = df.groupby(['A', 'B', 'C'])['% I'].mean()
subtotals = pivot_table.groupby(level=0).sum()
subtotals['% I'] = df.groupby('A')['% I'].mean()
grand_total = pivot_table.sum()
grand_total['% I'] = df['% I'].mean()
subtotals = subtotals.rename(index=lambda x: f'Sub Total ({x})')
pivot_table = pd.concat([pivot_table, subtotals])
pivot_table.loc['Grand Total'] = grand_total
pivot_table = pivot_table.reset_index()