Kishore L
Kishore L

Reputation: 188

How do I create a new column which is the max of another column where similar values grouped together in Pandas Dataframe?

I have a python dataframe as follows:

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

What I want to do is convert this into following dataframe:

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

Upvotes: 0

Views: 18

Answers (1)

BENY
BENY

Reputation: 323236

Check with transform

df['TotalSubmitted']=df.groupby('Student')['Assignment_submitted'].transform('count')

Upvotes: 2

Related Questions