You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于关键词匹配为DataFrame中的消息检测对应主题?

基于关键词为消息匹配对应主题

输入数据

主题-关键词映射表

Topic        Keyword
Math        ['plus','subtract','multiply','devide']
IT          ['computer','cpu', 'mainboard','monitor']
Chemistry   ['Water','Acid','Calcium','Soldium']
Biology     ['plant','DNA','gerne','evolution','chromosomes']

消息数据集

id      message
0       one plus one is two
1       buy me a computer
2       put calcium in acid
3       23 pairs of chromosomes

实现思路与代码

核心逻辑是遍历每条消息,检查是否包含某主题下的关键词,匹配到对应主题。为避免大小写干扰,统一转为小写处理,用Pandas实现如下:

import pandas as pd

# 构建主题-关键词DataFrame
topic_df = pd.DataFrame({
    'Topic': ['Math', 'IT', 'Chemistry', 'Biology'],
    'Keyword': [
        ['plus','subtract','multiply','devide'],
        ['computer','cpu', 'mainboard','monitor'],
        ['Water','Acid','Calcium','Soldium'],
        ['plant','DNA','gerne','evolution','chromosomes']
    ]
})

# 构建消息DataFrame
msg_df = pd.DataFrame({
    'id': [0,1,2,3],
    'message': [
        'one plus one is two',
        'buy me a computer',
        'put calcium in acid',
        '23 pairs of chromosomes'
    ]
})

# 统一转为小写,消除大小写匹配差异
topic_df['Topic_lower'] = topic_df['Topic'].str.lower()
topic_df['Keyword_lower'] = topic_df['Keyword'].apply(lambda x: [k.lower() for k in x])

# 定义主题匹配函数
def match_topic(message):
    msg_lower = message.lower()
    for _, row in topic_df.iterrows():
        if any(keyword in msg_lower for keyword in row['Keyword_lower']):
            return row['Topic']
    return None  # 无匹配时返回None

# 为每条消息匹配主题
msg_df['topic'] = msg_df['message'].apply(match_topic)

# 提取目标结果列
result = msg_df[['message', 'topic']]
print(result)

运行结果

message                   topic
one plus one is two       Math
buy me a computer         IT
put calcium in acid       Chemistry
23 pairs of chromosomes   Biology

内容的提问来源于stack exchange,提问作者llgx 10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 08:52:38