如何计算大Pandas的全能者

2024-09-28 21:09:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个像这样的数据帧

studentID subjectID subjectMark subjectWeight  Rank   overallScore 

 1         111         100         0.4           3      40      
 1         222         0           0.6           3      40   
 2         111         90          0.4           1      90      
 2         222         90          0.6           1      90     
 3         111         0           0.4           2      60      
 3         222         100         0.6           2      60       

如你所见,学生1和3在某些科目上都取得了很高的成绩,但他们的总成绩很差,而学生2在任何科目上都没有取得任何最高的成绩,但总成绩最好

总分=科目111分*科目111权重+科目222分*科目222权重

所以我想看看某个学生是否是“全能者”,也就是说,我想看看这个学生的总分是否最高,但在任何科目上都没有最高分。如果符合这个条件,就给这个学生贴上“全能”的标签

df应该是这样的:

studentID subjectID subjectMark subjectWeight  Rank   overallScore AR

 1         111         100         0.4           3      40         F
 1         222         0           0.6           3      40         F
 2         111         90          0.4           1      90         T
 2         222         90          0.6           1      90         T
 3         111         0           0.4           2      60         F
 3         222         100         0.6           2      60         F

我有一个后续问题
给出的答案可以在最后一个数据帧上实现,但是如果我想在接下来的数据帧中的每个类上都实现这个功能呢?你知道吗

studentID subjectID subjectMark subjectWeight  Rank   overallScore classID

 1         111         100         0.4           3      40         1
 1         222         0           0.6           3      40         1
 2         111         90          0.4           1      90         1
 2         222         90          0.6           1      90         1
 3         111         0           0.4           2      60         1
 3         222         100         0.6           2      60         1
 4         444         95          0.4           3      38         2
 4         555         0           0.6           3      38         2
 5         444         90          0.4           1      90         2
 5         555         90          0.6           1      90         2
 6         444         0           0.4           2      57         2
 6         555         95          0.6           2      57         2

Tags: 数据条件学生权重rank成绩科目全能
2条回答
list_of_all_rounder_per_class = []

for classid in data['classID'].unique():
    that_class = data.loc[data.classID == classID]
    condition1 = that_class.groupby(['subjectID']).subjectMark.transform('max').eq(that_class.subjectMark) 
    condition2 = that_class.overallScore.eq(that_class. overallScore.max()) 
    # get the above conditions and both met should return True
    list_of_all_rounder_per_class.append(condition2 &((~condition1).groupby(that_class['studentID']).transform('all')))

total_result = [result_for_each_class.to_frame('all_rounder') for result_for_each_class in list_of_all_rounder_per_class]
all_rounder = pd.concat(total_result)

data = data.join(all_rounder, how='outer')

我已经找到了一个解决这个问题的方法,尽管这可能是实现这个目标的最好(最干净的)方法

你可以查一下

s1=df.groupby('subjectID').subjectMark.transform('max').eq(df.subjectMark)# check the max score with each student 
s2=df.overallScore.eq(df.overallScore.max())# get the max score of overall
s2&((~s1).groupby(df['studentID']).transform('all'))# get the above conditions and both met should return True
Out[1066]: 
0    False
1    False
2     True
3     True
4    False
5    False
dtype: bool

相关问题 更多 >