简体   繁体   English

两个数据框之间的交叉检查?

[英]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)然后,我有另一个 df,每个 class(阈值)都有一个分数刻度

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我想根据分数等级为每个 class 的学生从他们的成绩中分配分数

How can I make a dataframe that looks like this:如何制作如下所示的 dataframe:

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.例如,如果某人的英语成绩为 16 分,他们将获得 3 分,而不是 4 分。

Use cut for all columns without first with bins and labels used from df2 :对所有列使用cut而不是 first 使用df2使用的 bins 和标签:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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