我有下面的数据集,我想计算"注释";每个学校,除了学校";B";,其中我希望等于零或缺少
student school notes nbr_of_student_per_school
1 A 12 45
1 A 13 45
2 A 10 45
3 B 13 -
4 C 16 46
5 A 10 45
6 C 20 46
7 C 10 46
8 B 11 -
df.groupby(['Country'])['notes'].sum()
试试这个:
df.query('school != "B"').groupby('school')['notes'].sum()
所以你只选择了学校不是B 的数据帧的子集
编辑:
另一种方法:评论:
# calculate mean
df['new_col'] = df.groupby('school')['notes'].transform('sum')
# now set B school sum to np.nan
df.loc[df['school'] == 'B', 'new_col'] = np.nan