This is crosstab:
(pd.crosstab([df['Department'], df['Semester']], df['Grade'])
.add_prefix('Grade ')
.reset_index()
)
which is a short-hand for groupby().value_counts()
then unstack
:
(df.groupby(['Department','Semester'])['Grade']
.value_counts()
.unstack(fill_value=0)
.reset_index()
)
Output:
Grade Department Semester Grade A Grade B Grade C
0 D1 Fall-2019 3 1 0
1 D1 Spring-2019 0 0 2
2 D1 Spring-2020 2 0 0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…