清理Python中不一致的数据类别

2024-05-14 00:20:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python中的数据框架,其中有一列用于大学学位类型,其中学位是以非常不一致的方式提到的。总共约有12654种独特的学位类型,手动分类需要很长时间

例如,学士学位可以被称为以下任何类型:

“理学士”、“工商管理学士”、“双学位”、“工学学士”、“法律学士”, “理学学士”,“应用科学学士(理学学士)”, “工程学士(BE)”,“学士”, "工程学士","理科学士",, “工商管理学士学位”, “理学学士”, “B.Bus”,“文学学士-文学学士”, “工商管理理学学士”, “理学学士”,“荣誉理学学士”,“理学学士”,“工程学士”,“通信学士”, “荣誉理学学士学位”, “学士”,“工商管理学士”, “Acc学士”,“荣誉文学学士学位”, “B.S.E.E.”、“L.L.B.”、“文科”、“理学士”、“学士学位”

硕士学位可以是以下任何类型:

“理学硕士”、“硕士”、“医学博士”, “硕士”、“文学硕士”、“工程硕士”、“法学硕士”、“文学硕士”、“科学硕士”, ‘PGDM’、‘高管教育’、‘毕业’, ‘LL.M’、‘MPH’、‘CA’、‘Diplom’、‘执行计划’, ‘SM’、‘L.L.M’、‘奖学金’、‘科学硕士’, “法学硕士”、“理学硕士”, “理学硕士”,“硕士学位”, “理学硕士”、“CFA”、“研究生文凭”、“理学硕士”, ‘MTech’、‘文学硕士’、‘CPA’, “工商管理硕士-工商管理硕士”, ‘教育硕士’、‘教育硕士’、‘专业化’、‘管理硕士’, “AMP”、“特许会计师”、“居住权”, “MED”、“MM”、“艺术大师”、“MASc”, ‘PGDBM’、‘MPS’、‘国际MBA’、‘MBA’, ‘M.Arch’、‘MIS’、‘MHA’、‘艺术硕士学位’, “MSci”、“PG”、“理工硕士”、“理学硕士”, “硕士”,“M.Phil.”,“硕士学位”

高中、副学士和博士等等

我想找到一条捷径,将他们大多数分为高中、副学士、学士、硕士或博士/博士

有人有什么建议吗

一些在线文章建议使用FuzzyWzzy之类的工具。我从来没有使用过这个工具,也不确定它是否真的对我有帮助

我对Python/数据科学非常陌生,不确定如何进行,因此请尽可能清楚地解释

感谢您,并期待解决方案:)


Tags: 数据类型科学工程博士学士学位学士硕士学位
1条回答
网友
1楼 · 发布于 2024-05-14 00:20:20

以下是我对你问题的评论:

第一个块为我提供了一个数据帧,类似于我想象中的您的数据帧:

import pandas as pd
import numpy as np

your_list = np.array(['Sc.B','S.B.','Dual Degree','BEng. 2master','M.Phil.','Masters degree'])
names = np.array([f"person_{ii}" for ii in range(len(your_list))])

df = pd.DataFrame({"names": names, "degree_title": your_list})
print(df)

现在我们可以对学位标题数据进行for循环,我们的第一个猜测如下 达到学位课程的样子

new_classifications = [] # Make an empty list so we can keep track of what we classify the new degree as.

for degree in df["degree_title"]:
    if "bachelor" in degree.lower(): # lower() as we don't care if it's "Bachelor" or "bachelor"
        new_classifications.append("bachelor") # Anything here is good enough to be called "bachelor"
    elif "master" in degree.lower():
        new_classifications.append("master")
    elif "doctorate" in degree.lower():
        new_classification.append("phd")
    else:
        new_classifications.append("unclassified")
        print(f"no classification found for {degree}")

这告诉我们,我们缺少像B.Sc这样的大量结果,因此我们可以为第二次尝试中的结果添加检查-注意“学士”和“硕士”行中的添加

请注意,有一行是“边缘案例”-从标题中我无法猜测“专业化”是硕士水平的资格,因此我们必须“手动”完成此操作

new_classifications = [] 

for degree in df["degree_title"]:
    if "bachelor" in degree.lower() or degree.lower().startswith("b") or "b." in degree.lower():
        new_classifications.append("bachelor")
    elif "B" in degree and degree.isupper(): # Also require the whole title to be uppercase 
        new_classifications.append("bachelor")
    elif "master" in degree.lower() or degree.lower().startswith("m") or "m." in degree.lower():
        new_classifications.append("master")
    elif "M" in degree and degree.isupper():
        new_classifications.append("master")
    elif "doctorate" in degree.lower():
        new_classification.append("phd")
    elif degree in ["Diplom", "Fellowship", "CPA", "Specialisation", "Graduate Diploma"]:
        new_classifications.append("some_classification_that_you_write_for_these_edge_cases")
    else:
        new_classifications.append("unclassified")
        print(f"no classification found for {degree}")

当我们高兴时,我们可以向数据帧中添加分类良好的度

df["new_classification"] = new_classifications
print(df)

这是一种非常“蛮力”的方法来解决这个问题,但考虑到许多学位头衔将遵循类似的模式,这是一种非常简单的方法来开始,并删除大量的工作,留下更少的手工分类

相关问题 更多 >