用python进行excel全文词频统计,并标记是哪一分类

import pandas as pd
import jieba
from collections import Counter

# 读取 Excel 文件
df = pd.read_excel('your_excel_file.xlsx')

# 定义函数用于分词和统计词频
def word_freq(content):
    # 对文章内容进行分词
    words = jieba.cut(content)
    # 统计词频
    return Counter(words)

# 创建新的数据框
new_df = pd.DataFrame(columns=['智库名称', '主题', '词语', '词频'])

# 遍历每个智库名称和主题
for think_tank in df['think_tank_name'].unique():
    for topic in df['type'].unique():
        # 筛选符合条件的行,并将文章内容合并为一个字符串
        temp_df = df[(df['think_tank_name']==think_tank) & (df['type']==topic)]
        content = ''.join(temp_df['art_content'].tolist())
        # 进行分词和统计词频
        freq = word_freq(content)
        # 将统计结果写入新数据框
        for word, count in freq.items():
            new_df = new_df.append({
                '智库名称': think_tank,
                '主题': topic,
                '词语': word,
                '词频': count
            }, ignore_index=True)

# 将新数据框存储到 Excel 文件中
new_df.to_excel('new_excel_file.xlsx', index=False)

用python,要根据excel的think_tank_name列下不同智库,art_content列下是文章内容,type列下有不同主题。词频统计根据的是文章内容,把不同智库的每个主题的词频统计放到新建的excel表里

猜你喜欢

转载自blog.csdn.net/EaSoNgo111/article/details/129995417