【问题标题】:Searching for keyword combinations in pandas dataframe for classification在熊猫数据框中搜索关键字组合进行分类
【发布时间】:2022-11-16 21:31:01
【问题描述】:

这是Searching for certain keywords in pandas dataframe for classification 的后续问题。

我有一个关键字列表,我想根据这些关键字对职位描述进行分类。这是输入文件,示例关键字和代码

job_description
Managing engineer is responsible for
This job entails assisting to
Engineer is required the execute
Pilot should be able to control
Customer specialist advices
Different cases brought by human resources department


cat_dict = {
    "manager": ["manager", "president", "management", "managing"],
    "assistant": ["assistant", "assisting", "customer specialist"],
    "engineer": ["engineer", "engineering", "scientist", "architect"],
    "HR": ["human resources"]
}

def classify(desc):
    for cat, lst in cat_dict.items():
        if any(x in desc.lower() for x in lst):
            return cat

df['classification'] = df["job_description"].apply(classify)

如果只有一个词,例如,代码运行良好。 “经理”或“助理”,但当有两个词时无法识别情况,例如“客户专家”或“人力资源”

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    我认为您的 cat_dict 字典中缺少逗号。我试过你的例子:

    import pandas as pd
    
    cat_dict = {
        "manager": ["manager", "president", "management", "managing"],
        "assistant": ["assistant", "assisting", "customer specialist"],
        "engineer": ["engineer", "engineering", "scientist", "architect"],
        "HR": ["human resources"]
    }
    
    def classify(desc):
        for cat, lst in cat_dict.items():
            if any(x in desc.lower() for x in lst):
                return cat
    
    text_df = pd.Series(text.split('
    ')[1:])
    text_df.apply(classify)
    

    结果:

    0      manager
    1    assistant
    2     engineer
    3         None
    4    assistant
    5           HR
    dtype: object
    

    成功地将助理归类为“客户专家”,将 HR 归类为“人力资源”。

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 2016-12-17
      • 2017-02-22
      • 2017-08-12
      • 2020-06-25
      • 2022-01-25
      • 2017-06-03
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多