简体   繁体   中英

Groupby sum of col1 divided by sum of col2

I have DF like this:

在此处输入图像描述

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

在此处输入图像描述

How can I do it?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM