我正在通过 .csv 文件对调查中收到的数据进行预处理。此列包含学生所学习的课程名称。由于这是由他们输入的,因此相同的课程名称有不同的拼写方式。例如:课程名称“BALLB”已被输入,如“Ballb”或“bal.lb”等。我尝试了最基本的蛮力方法,我能想到我在 if 语句中采用所有选项的位置和用通用的课程拼写替换它们,但我仍然得到大量的值,该程序无法将它们组合到其中一个语句中。有没有更快的方法将它们组合在一起?
def get_course_name(x):
if 'B.E' in x or 'B.E.' in x or 'BE' in x or 'B.E(cse)' in x or 'Bachelor Of Engineering' in x or 'BECSE' in x or 'Be' in x:
return 'B.E.'
if 'L.L.B.' in x or 'Ballb(h)' in x or 'Ballb' in x:
return 'B.A. LLB'
if 'B.Tech' in x or 'B.TECH' in x or 'B.tech' in x or 'B .Tech' in x or 'Btech' in x or 'BTech' in x or 'B-tech' in x or 'B.Tech.' in x or 'CSE' in x or 'Biotechnology' in x or 'Biotech' in x:
return 'B. Tech'
if 'B.pharmacy' in x or 'B. Pharmacy' in x or 'B pharma' in x or 'pharmacy' in x or 'B.Pharmacy' in x or 'M.pharmacy' in x or 'B.Pharm' in x or 'Pharma' in x or 'pharm' in x or 'Pharmacy' in x or 'B.pharma' in x or 'B-pharmacy' in x:
return 'B. Pharma'
if 'BBA' in x or 'bba' in x:
return 'BBA'
if 'MBA' in x or 'mba' in x or 'Mba' in x or 'MBA ' in x:
return 'MBA'
if 'M.Tech' in x or 'M. Tech' in x or 'mtech' in x or 'm.tech' in x or 'M-tech' in x or 'Mtec-EE' in x:
return 'M. Tech'
if 'MBBS' in x or 'mbbs' in x:
return 'MBBS'
if 'B.Sc' in x or 'B. Sc' in x or 'Bsc.' in x or 'B.S.c' in x:
return 'B. Science'
if 'msc' in x or 'M.Sc' in x or 'M. Sc' in x or 'Msc' in x or 'MSc' in x or 'm.sc' in x:
return 'M. Science'
return 'misc'
这就是我调用函数来获取每门课程的价值计数的地方:
df1['Course Name'] = df1['Course Name'].apply(get_course_name)
df1['Course Name'].value_counts()
这就是数据框的样子
我要分组的列称为“课程名称”。