I have 2 csv files as below, I want to find if an individual performance (in df1) is above/below class average (in df2) using come compare function after finding their values.
df1:
Name Class Test1 Test2 Test3
John 9A 75 83 77
David 9B 65 67 55
Peter 9A 85 90 88
Tom 9C 74 92 78
df2:
Class Test1 Test2 Test3
9A 80 82 84
9B 84 75 77
9C 75 78 80
Here's my method, feel free to correct/guide me if I'm wrong. I first find the Class
of an individual in df1
, e.g., John
is 9A
, then return the other columns such as Test1
or Test2
in df2
based on 9A
target_class = df1.loc[df1['Name'] == 'John', 'Class']
print(target_class)
>>>>9A
Test1_avg = df2.loc[df2['Class'] == target_class, 'Test1']
# ideally it should return 80
And I got this ValueError: Can only compare identically-labeled Series objects
Or simply, how would you compare John's Test1 in df1 vs Class 9A's Test1 in df2? Is there any easier method than mine? Thanks for your help!
Update: I'll then use a compare function like this to return a score if it fulfills the criteria
def comparison(a, b):
return 2 if a > b else 1 if a == b else -1