Python 带有Groupby的百分比列
我有一个数据帧Python 带有Groupby的百分比列,python,pandas,Python,Pandas,我有一个数据帧 Student Exam Month 1 Maths 10 1 Maths 10 1 Maths 11 1 Science 10 1 Physics 10 2 science 11 2 physics 11 2 Maths 12 我想要每个学
Student Exam Month
1 Maths 10
1 Maths 10
1 Maths 11
1 Science 10
1 Physics 10
2 science 11
2 physics 11
2 Maths 12
我想要每个学生每个月每个考试的输出百分比
Student Exam Month Pourcentage
1 Maths 10 50%
1 Maths 11 100%
1 Science 10 25%
1 physics 10 25%
2 physics 11 50%
2 science 11 50%
2 Maths 12 100
我试图使用这段代码,但它没有给出正确的结果
Count_exam= df.groupby('student','Exam')[Month].count()
Count_month= df.groupby('student')[Month].count()
df['Pourcentage] = Count_exam * 100 /Count_month
IIUC您可以简单地除以每组的
大小
:
df["Pct"] = 1/df.groupby(["Student", "Month"])["Exam"].transform("size")*100
print (df.groupby(["Student", "Exam", "Month"], as_index=False).agg("sum"))
Student Exam Month Pct
0 1 Maths 10 50.0
1 1 Maths 11 100.0
2 1 Physics 10 25.0
3 1 Science 10 25.0
4 2 Maths 12 100.0
5 2 physics 11 50.0
6 2 science 11 50.0
它给了你什么?请求帮助时请包括所有详细信息。