You need to enable JavaScript to run this app. 如何基于Pandas DataFrame每行指定词表统计对应文本列的词频
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Pandas DataFrame每行指定词表统计对应文本列的词频

实现方案

你可以按以下步骤实现需求,完整可运行代码如下:

import pandas as pd
import ast

# 你的示例DataFrame
df = pd.DataFrame({
    "id":[
        "100",
        "200",
        "300"
    ],
    "text":[
        "The best part of Zillow is you can search/view thousands of home within a click of a button without even stepping out of your door.At the comfort of your home you can get all the details such as the floor plan, tax history, neighborhood, mortgage calculator, school ratings etc. and also getting in touch with the contact realtor is just a click away and you are scheduled for the home tour!As a first time home buyer, this website greatly helped me to study the market before making the right choice.",
        "I love all of the features of the Zillow app, especially the filtering options and the feature that allows you to save customized searches.",
        "Data is not updated spontaneously. Listings are still shown as active while the Mls shows pending or closed."
    ],
    "word":[
        "[best, word, door, subway, rain]",
        "[item, best, school, store, hospital]",
        "[gym, mall, pool, playground]",
    ]
})

# 1. 将word列的字符串格式列表转换为真实Python列表
df['word'] = df['word'].apply(ast.literal_eval)

# 2. 自定义行处理函数,统计每行指定词的出现频率
def count_row_word_freq(row):
    text = row['text']
    target_words = row['word']
    freq_dict = {}
    for word in target_words:
        # 若需要忽略大小写统计,可改为 text.lower().count(word.lower())
        freq_dict[word] = text.count(word)
    return freq_dict

# 3. 生成最终结果
result_df = pd.DataFrame({
    'id': df['id'],
    'word dict': df.apply(count_row_word_freq, axis=1)
})

# 打印查看结果
print(result_df)

运行后输出的result_df完全符合你要求的格式。

补充:如果需要精确匹配整词(避免子串误统计,比如he不会被the命中),可以引入re库实现:

import re
# 把统计行替换为以下代码即可
freq_dict[word] = len(re.findall(rf'\b{re.escape(word)}\b', text))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.21 22:36:11