'Cross check between two data frames?
I have a df of students with numeric grades in different classes. Then, I have another df that that has a points scale for each class (thresholds)
Students Math English
Josh 45 27
Max 23 37
Justin 34 13
Charles 56 27
Points scale
points Math English
1 10 5
2 20 10
3 30 15
4 40 20
5 50 25
I want to assign points to different students from their grades for each class based off of the points scale
How can I make a dataframe that looks like this:
Students Math English
Josh 4 5
Max 2 5
Justin 3 2
Charles 5 3
If someone has an English score of 16 for example, they would get 3 points, not 4.
Solution 1:[1]
Use cut for all columns without first with bins and labels used from df2:
for c in df1.columns[1:]:
df1[c] = pd.cut(df1[c], bins=df2[c].tolist() + [np.inf], labels=df2['points'])
print (df1)
Students Math English
0 Josh 4 5
1 Max 2 5
2 Justin 3 2
3 Charles 5 5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jezrael |
