赞
踩
部分代码
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams[“font.sans-serif”] = ‘SimHei’
plt.rcParams[‘axes.unicode_minus’] = False
data = pd.read_csv(“【数据】大众点评网餐厅口碑数据.csv”, encoding = “gbk”)
print(f"===成功导入\t{data.shape[0]}行数据")
print(data.info())
print()
print(data.isnull().sum())
print()
data1 = data[[‘District’, ‘ApplauseRate’]] #提取需要的列
print(data1)
data2 = data1.groupby(data[‘District’]).mean() #分类汇总取平均值
print(data2)
data2 = data2.sort_values(by = ‘ApplauseRate’, ascending=False) #排序
print(data2)
def show_ApplauseRate_by_District():
global data2
data2 = data2.reset_index() print(data2) x = data2.District y = data2.ApplauseRate plt.figure(figsize=(15,7)) plt.bar(x, y, width = 0.5, align = "center", label = "平均5星好评率") title = "图1 - 平均5星好评率-District分布" plt.title(title, loc="center", fontsize=16) t = [f'{y1:.02f}' for y1 in y] #取两位小数显示 for index in range(len(x)): plt.text(x[index], y[index], t[index], ha = "center", va = "bottom", fontsize = 10) #设置x和y轴的名称 plt.xlabel("District", fontsize=12) plt.ylabel("平均5星好评率", fontsize=12) plt.legend(loc='best', fontsize = 12) plt.savefig(title+".png", dpi=300) plt.show()
show_ApplauseRate_by_District()
运行效果
百度网盘获取完整源码及数据
链接:https://pan.baidu.com/s/1Xwm8NEmbG6UyyggZLoEOQw?pwd=wsk7
提取码:wsk7
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。