目录
一、案例背景
2025 年全国两会在 “十四五” 规划收官的关键节点召开,备受各界瞩目。会议聚焦经济、民生、科技等多元领域,释放出众多重要政策信号。从经济增长目标的设定,到财政政策的调整;从科技创新的推进,到民生保障的强化,都与社会发展和民众生活息息相关。本案例借助 Python 对两会相关数据进行深入剖析,旨在挖掘热点议题背后的趋势和潜在影响,为政策解读和决策参考提供数据支持。
二、代码实现
import requests
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
import json
2.1 数据收集
从新闻媒体网站、政府官方文件网站、社交媒体平台等多渠道收集两会相关数据。这里以模拟从新闻媒体 API 和社交媒体数据文件获取数据为例。
# 模拟从新闻媒体API获取两会新闻报道数据
news_api_url = 'https://news_api.com/two_sessions_news'
news_api_headers = {
'Authorization': 'Bearer your_news_api_token'
}
news_response = requests.get(news_api_url, headers=news_api_headers)
news_data = news_response.json()
news_df = pd.DataFrame(news_data['news'])
# 从社交媒体获取两会相关话题讨论数据
with open('social_media_two_sessions.json', 'r', encoding='utf-8') as f:
social_media_data = json.load(f)
social_media_df = pd.DataFrame(social_media_data)
2.2 数据探索性分析
查看数据基本信息,了解数据类型、缺失值情况,统计新闻报道中提及最多的议题,分析社交媒体讨论数据中热门话题和高频词汇。
# 查看新闻报道数据基本信息
print(news_df.info())
# 查看社交媒体讨论数据前几行
print(social_media_df.head())
# 统计新闻报道中提及最多的议题
from collections import Counter
news_topic_counter = Counter(news_df['topic'])
top_news_topics = news_topic_counter.most_common(10)
print(top_news_topics)
# 统计社交媒体讨论数据中提及频率较高的关键词
social_text = ' '.join(social_media_df['text'])
words = social_text.split()
word_counter = Counter(words)
top_words = word_counter.most_common(10)
print(top_words)
2.3 数据清洗
处理新闻报道数据中的缺失值,如对重要字段进行填充或删除;对社交媒体讨论数据的文本进行清洗,去除特殊字符、停用词,统一文本格式。
# 处理新闻报道数据的缺失值,填充重要字段为'Unknown'
important_fields = ['title', 'topic', 'content']
for field in important_fields:
news_df[field] = news_df[field].fillna('Unknown')
# 处理社交媒体讨论数据的文本,去除特殊字符和停用词
import re
from nltk.corpus import stopwords
import nltk
nltk.download('stopwords')
stop_words = set(stopwords.words('english'))
def clean_text(text):
text = re.sub(r'[^a-zA-Z]', ' ', text)
text = text.lower()
words = text.split()
filtered_words = [word for word in words if word not in stop_words]
return ' '.join(filtered_words)
social_media_df['clean_text'] = social_media_df['text'].apply(clean_text)
2.4 数据分析
2.4.1 热点议题关注度分析
统计不同渠道(新闻报道、社交媒体)对各热点议题的提及次数,以柱状图对比展示。
# 统计新闻报道中各热点议题的提及次数
news_topic_count = news_df['topic'].value_counts()
# 统计社交媒体讨论中各热点议题的提及次数
social_media_topic_count = social_media_df['topic'].value_counts()
# 合并数据
topic_count_df = pd.DataFrame({
'News