Home > Software engineering >  Groupby sum of col1 divided by sum of col2
Groupby sum of col1 divided by sum of col2

Time:12-15

I have DF like this:

enter image description here

and I want to calculate how much is students was prepared via his overall score in the exam, like this:

enter image description here

How can I do it?

Edit: the answer given by @user17242583 is right, but, the values are stacked in the first rows, how to implement each value to each needed row ? enter image description here

CodePudding user response:

You probably want something like this:

s = df.groupby(['Student', 'Exam']).apply(lambda group: (f'{group["IsCorrect"].sum()}\\{group.shape[0]}', (group['IsCorrect'].sum() / group.shape[0]))).reset_index(drop=True)
df['prepared'] = s.str[0]
df['prepared percentage'] = s.str[1]
  • Related