提问过程:
豆包处理过程:
我将通过统计好友所在国家和省份的分布情况,用合适的图表进行可视化展示。
import pandas as pd
import matplotlib.pyplot as plt
# 加载数据集
df = pd.read_csv('/mnt/wyt微信好友.csv')
print('数据基本信息:')
df.info()
# 查看数据集行数和列数
rows, columns = df.shape
if rows < 100 and columns < 20:
# 短表数据(行数少于100且列数少于20)查看全量数据信息
print('数据全部内容信息:')
print(df.to_csv(sep='\t', na_rep='nan'))
else:
# 长表数据查看数据前几行信息
print('数据前几行内容信息:')
print(df.head().to_csv(sep='\t', na_rep='nan'))
基于执行结果可知,数据集包含 100 行 15 列数据,参与分析的字段为国家
和省份
,其中国家
列有 87 个非空值,省份
列有 60 个非空值。
接下来,我会统计不同国家
和省份
的好友数量,然后用柱状图进行可视化展示,以直观呈现好友在不同国家和省份的分布情况。
# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei']
# 统计不同国家的微信好友数量
country_counts = df['国家'].value_counts()
# 统计不同省份的微信好友数量
province_counts = df['省份'].value_counts()
# 创建两个图像
fig, axs = plt.subplots(2, figsize=(10, 10))
# 绘制不同国家的微信好友数量柱状图
bars1 = axs[0].bar(country_counts.index, country_counts.values)
# 为柱状图添加数据标签
for bar in bars1:
height = bar.get_height()
axs[0].text(bar.get_x() + bar.get_width() / 2, height, f'{height}', ha='center', va='bottom')
# 设置标题和标签
axs[0].set_title('不同国家的微信好友数量')
axs[0].set_xlabel('国家')
axs[0].set_ylabel('微信好友数量')
# 旋转 x 轴标签以便更好显示
axs[0].tick_params(axis='x', rotation=90)
# 绘制不同省份的微信好友数量柱状图
bars2 = axs[1].bar(province_counts.index, province_counts.values)
# 为柱状图添加数据标签
for bar in bars2:
height = bar.get_height()
axs[1].text(bar.get_x() + bar.get_width() / 2, height, f'{height}', ha='center', va='bottom')
# 设置标题和标签
axs[1].set_title('不同省份的微信好友数量')
axs[1].set_xlabel('省份')
axs[1].set_ylabel('微信好友数量')
# 旋转 x 轴标签以便更好显示
axs[1].tick_params(axis='x', rotation=90)
# 自动调整子图参数,使之填充整个图像区域
plt.tight_layout()
# 显示图形
plt.show()
基于执行结果,已成功绘制柱状图展示不同国家
和省份
的微信好友数量。通过这两个柱状图,可以直观地比较各个国家和省份的好友数量分布情况。