You could do this with groupby
+ agg
. For your exact output with Y
and N
at each level, you'd need reindex
:
g = df.groupby(['Name', 'Attended'], sort=False).Points.agg(['count', 'sum'])
g
count sum
Name Attended
Smith Y 2 49.38
N 1 0.00
Adams N 1 0.00
Y 1 33.43
Morgan Y 2 54.38
idx = pd.MultiIndex.from_product([g.index.levels[0], ['Y', 'N']])
idx
MultiIndex(levels=[['Adams', 'Morgan', 'Smith'], ['N', 'Y']],
labels=[[2, 2, 0, 0, 1, 1], [1, 0, 1, 0, 1, 0]])
g.reindex(idx, fill_value=0)
count sum
Smith Y 2 49.38
N 1 0.00
Adams Y 1 33.43
N 1 0.00
Morgan Y 2 54.38
N 0 0.00
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…