我有一个python数据框,如下所示:
AssignmentID Student Assignment_submitted Passed
1 stud1 1 1
2 stud2 1 1
3 stud3 1 1
4 stud1 2 0
5 stud4 1 1
6 stud1 3 0
我要做的是将其转换为以下数据框:
AssignmentID Student Assignment_submitted Passed TotalSubmitted
1 stud1 1 1 3
2 stud2 1 1 1
3 stud3 1 1 1
4 stud1 2 0 3
5 stud4 1 1 1
6 stud1 3 0 3
答案 0 :(得分:2)
使用transform
df['TotalSubmitted']=df.groupby('Student')['Assignment_submitted'].transform('count')