python 11Pandas数据可视化实验

实验目的:

学会使用Pandas操作数据集,并进行可视化。

数据集描述:

该数据集是CNKI中与"中药毒理反应"相关的文献信息,包含文章题目、作者、来源(出版社)、摘要、发表时间等信息。

实验要求:

  1. 使用Pandas读取数据集。
  2. 统计每年的发文数量,并绘制折线图。
  3. 统计出版社的发文量信息,列出发文前10位的出版社。
  4. 使用jieba分词,对摘要进行分词统计,制作词频前30位的词云图。(需安装jieba分词和词云工具包)。

实验过程:

为了完成这个实验,你需要在你的Python环境中安装Pandas, jieba, matplotlib, 和 wordcloud这几个库。如果你还没有安装,可以通过以下命令进行安装:

python 复制代码
pip install pandas jieba matplotlib wordcloud

导入库,导入文件(文件名称为 ansi.csv),打印几行看看导入情况

python 复制代码
import pandas as pd
df = pd.read_csv('./ansi.csv')
df.head()

通过输出查看文件的表头是什么

python 复制代码
print(df.columns)

统计每年的发文数量,并绘制折线图

python 复制代码
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
df['年'] = pd.to_datetime(df['5'], format='%Y-%m-%d %H:%M', errors='coerce').dt.year
year = df['年'].value_counts().sort_index()
plt.figure(figsize=(10, 6))
plt.plot(year.index, year.values, marker='o')
plt.title('每年中药毒理反应文献发表数量')
plt.xlabel('年份')
plt.ylabel('发表数量')
plt.grid(True)
plt.show()

统计出版社的发文量信息,列出发文前10位的出版社

python 复制代码
publisher = df['2'].value_counts()
top = publisher.head(10)
print("发文前10位的出版社:")
print(top)

使用jieba分词,对摘要进行分词统计,制作词频前30位的词云图

python 复制代码
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt


def tokenize_abstracts(abstracts):
    all_words = []
    for abstract in abstracts:
        words = jieba.cut(abstract, cut_all=False)
        all_words.extend(words)
    return all_words

abstracts = df['4'].dropna().tolist()

# 分词
all_words = tokenize_abstracts(abstracts)

# 计算词频
word_freq = {word: all_words.count(word) for word in set(all_words)}

# 制作词云
wordcloud = WordCloud(font_path='simhei.ttf',width=800, height=600, background_color='white', max_words=30).generate_from_frequencies(word_freq)

# 显示词云图
plt.figure(figsize=(10, 8))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
相关推荐
SelectDB12 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码20 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵1 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li1 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸2 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学2 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田2 天前
Pydantic校验配置文件
python
hboot2 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi3 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi3 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab