如何从大小不同的不同文件中进行匹配

问题描述 投票:1回答:1

我有一种这样的数据:

course_ID subject_IDS
C242 E434\tT423\tS32
C98 F8\tW109\tU275\tV33

其中每个subject_IDScourse_ID的数量不同(也许一个路线有一个,而另一个路线有一个以上)。对于每个subject_IDS,都有一个包含以下内容的文件(这些文件应包含相同的学生ID):

student_IDs\tstudent_IDs\thas_this_subject_or_not
124\t124\t2
54\t54\t1
832\t832\t2
99\t99\t1

[其中1判定具有此student_ID的该学生没有那个科目,而2则相反。

我需要遍历subject_ID中的每个course_ID,最后有一个文件来确定具有该ID的学生是否已为该课程学习了这些科目中的任何一个(例如,通过将OR转换为2首先执行0;因此最后将是OR,因为该课程中有这些主题中的任何一个,否则为0

任何帮助将不胜感激。

python pandas dictionary set logical-or
1个回答
0
投票

您可以创建一个熊猫数据框,在其中合并所有文件,然后每一行包含:1course_IDsubject_IDstudent_ID。然后按照您的建议将has_this_subject_or_not列转换为布尔值列。现在您可以在has_this_subject_or_notcourse_ID上分组:

student_ID

输出

# simplified data:
data = [[1, 1, 1, 0], [1, 2, 1, 1], [2, 1, 1, 0]]
cols = ['course_ID', 'subject_ID', 'student_ID', 'has_this_subject_or_not']
df_combined = pd.DataFrame(data, columns=cols)
df_combined.groupby(by=['course_ID', 'student_ID']).has_this_subject_or_not.sum() > 0
© www.soinside.com 2019 - 2024. All rights reserved.