用python做词频统计、词云绘制

import pandas as pd
import requests
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 读取Excel文件
df = pd.read_excel("D:/Working/Suncent/DataCleaning/滤清/滤清-Amazon评论明细报表.xlsx")
df = df[df['评论星级']<4]
count_ = len(df)
# 将评论内容转为字符串并去除缺失值
comments = df["评论内容"].astype(str).dropna()

# 定义一个空的Counter对象
word_count = Counter()

# 无意义的词列表
stop_words = ['which','even','also','than',"not",'as','too',"it's",'when','if','so','just','from','it','any',"a", "an", "the", "and", "or", "but", "to", "of", "in", "on", "at", "for", "with", "is", "are", "was", "were", "be", "been", "this", "that", "these", "those", "can", "could", "may", "might", "will", "would", "should", "shall", "must", "had", "has", "have", "do", "does", "did", "it", "its", "they", "their", "them", "he", "him", "his", "she", "her", "hers", "you", "your", "yours", "we", "our", "us", "ours", "i", "me", "my", "mine"]

# 遍历所有评论,统计词频
for comment in comments:
    # 将每个评论按空格分割成单词列表,并去除无意义单词
    comment = comment.replace(".", "")
    words = [word.lower() for word in comment.split() if word.lower() not in stop_words]
    # 将单词列表转为Counter对象
    word_count.update(words)

# 定义一个空的DataFrame对象,用于保存结果
result_df = pd.DataFrame(columns=["词组", "频数", "频率"])

# 遍历所有词组,进行翻译并保存结果
for word, count in word_count.items():
    # # 调用百度翻译API进行翻译
    # res = requests.get(f"http://fanyi.baidu.com/transapi?from=en&to=zh&query={word}")
    # # 解析翻译结果
    # trans_word = res.json()["data"][0]["dst"]

    # 计算词频率
    freq = count / len(comments)
    # 将结果保存到DataFrame对象中
    # result_df = result_df.append({"词组": word, "频数": count, "频率": freq}, ignore_index=True)
    result_df = pd.concat([result_df, pd.DataFrame({"词组": [word], "频数": [count], "频率": [freq]})], ignore_index=True)
result_df = result_df.sort_values(by='频数', ascending=False)
# 将结果导出到Excel文件中
result_df.to_excel("D:/Working/Suncent/DataCleaning/滤清/滤清-词频统计(去掉介词等,星级低于4).xlsx", index=False)

import matplotlib.pyplot as plt
import seaborn as sns

# 设置字体为 Microsoft YaHei
plt.rcParams['font.family'] = 'Microsoft YaHei'

# 设置调色板
sns.set_palette("colorblind")

# 绘制英文词云图
wordcloud = WordCloud(background_color="white", width=6000, height=5000,max_words=500).generate_from_frequencies(word_count)
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.title(f'来自滤清器{count_}条低于4星的评论')
plt.show()
#
# # 绘制中文词云图
# zh_word_count = Counter(result_df["翻译后的词组"])
# zh_wordcloud = WordCloud(background_color="white", font_path="simhei.ttf").generate_from_frequencies(zh_word_count)
# plt.imshow(zh_wordcloud, interpolation="bilinear")
# plt.axis("off")
# plt.show()

这段代码是一个Python脚本,用于分析和可视化一个Excel文件中的评论数据。以下是对代码每一步的详细解释:

  1. 导入必要的库:

    • pandas:用于数据处理和分析。
    • requests:用于发送HTTP请求。
    • collections.Counter:用于计数对象。
    • wordcloud:用于生成词云图。
    • matplotlib.pyplot:用于绘图。
    • seaborn:用于增强的绘图功能。
  2. 读取Excel文件:

    • 使用 pandasread_excel 函数读取指定路径下的Excel文件。
    • 过滤出评论星级小于4的行。
  3. 计算过滤后的评论数量:

    • 使用 len(df) 计算过滤后的评论数量,并将其存储在变量 count_ 中。
  4. 将评论内容转换为字符串并去除缺失值:

    • 使用 astype(str) 将评论内容转换为字符串类型。
    • 使用 dropna() 去除任何缺失值。
  5. 定义一个空的 Counter 对象 word_count 用于统计词频。

  6. 定义一个无意义词列表 stop_words,这些词在词频统计时会被忽略。

  7. 遍历所有评论,统计词频:

    • 遍历 comments 序列中的每个评论。
    • 将评论中的句号去除,并将其转换为小写,然后按空格分割成单词列表。
    • 过滤掉列表中的无意义单词。
    • 使用 Counterupdate 方法更新词频。
  8. 定义一个空的 DataFrame 对象 result_df,用于保存词组、频数和频率。

  9. 遍历 word_count 中的所有词组,计算频率并保存结果:

    • 遍历 word_count 中的每个词和对应的计数。
    • 计算每个词的频率(词频除以评论总数)。
    • 将词组、频数和频率添加到 result_df 中。
    • result_df 按频数降序排序。
  10. 将结果导出到Excel文件中:

    • 使用 to_excel 函数将 result_df 导出到指定路径的Excel文件中。
  11. 设置绘图字体和调色板:

    • 设置 matplotlib 的字体为 Microsoft YaHei,以便正确显示中文。
    • 设置 seaborn 的调色板为 colorblind
  12. 绘制英文词云图:

    • 使用 WordCloud 类生成词云图。
    • 使用 imshow 函数显示词云图。
    • 隐藏坐标轴。
    • 设置图表标题并显示图表。
  13. 注释掉的代码块是用于绘制中文词云图的,但由于缺少中文字体路径和翻译API的实现,这部分代码被注释掉了。

注意:代码中有一些潜在的问题和改进点:

  • 代码中的百度翻译API调用被注释掉了,如果需要使用,需要取消注释并提供有效的API密钥。
  • result_df 的构建使用了 pd.concat,这在循环中可能不是最高效的方法,可以考虑先构建一个列表,然后在循环结束后一次性转换为 DataFrame
  • 代码中的词云图绘制部分没有使用 seaborn,而是直接使用了 matplotlib
  • 代码中的中文词云图绘制部分被注释掉了,如果需要使用,需要提供中文字体路径和实现翻译功能。
本篇文章介绍使用Python进行英文文献词频统计词云绘制的方法。 1. 数据收集 首先需要收集要分析的英文文献数据。可以从网上搜索并下载文献的text或者pdf格式文件。 2. 数据处理 使用Python读入文献文件,并根据需要进行数据清洗和预处理,例如去除标点符号、停用词和数字等。可以使用nltk库进行自然语言处理。 import string from nltk.corpus import stopwords from nltk.tokenize import word_tokenize from collections import Counter #读入文献文件 with open(r"path/to/file.txt","r", encoding="utf-8") as f: text = f.read() #去除标点符号和数字 text = text.translate(str.maketrans("", "", string.punctuation+string.digits)) #分词并去除停用词 tokens = word_tokenize(text.lower()) stop_words = set(stopwords.words("english")) filtered_words = [word for word in tokens if word not in stop_words] 3. 数据分析 使用Python统计词频,并将结果保存为字典或DataFrame,方便后续绘制词云或更进一步的分析。 #计算词频 word_count = Counter(filtered_words) #将结果保存为pandas DataFrame import pandas as pd df = pd.DataFrame.from_dict(word_count, orient='index', columns=['count']) 4. 词云绘制 使用Python绘制词云,可以使用wordcloud库。在绘制词云的时候可以对每个单词附上对应的词频。 #导入wordcloud库 from wordcloud import WordCloud import matplotlib.pyplot as plt #将结果转换为字典格式 word_dict = {} for key in word_count: word_dict[key] = word_count[key] #绘制词云 wc = WordCloud(background_color="white", max_words=1000) wc.generate_from_frequencies(word_dict) plt.figure(figsize=(12,6)) plt.imshow(wc, interpolation='bilinear') plt.axis("off") plt.show() 完成以上代码后,就可以得到类似如下的词云图: 如图所示,大的词表示在文献中出现的频率更高的单词。通过词云来直观地展现文献中的主题和关键词,有助于提高阅读效率和理解文章主旨。 本篇文章介绍了如何使用Python进行英文文献词频统计词云绘制。通过这些技术,不仅可以快速获取文献中的关键词,也可以更深入地理解文献的内容和结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐木叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值