|
|
import pandas as pd
|
|
|
from pyecharts.charts import Line,Pie,Bar,WordCloud
|
|
|
from pyecharts import options as opts
|
|
|
|
|
|
# 读取Excel文件
|
|
|
df = pd.read_excel('携程餐厅数据.xlsx', skiprows=[1]) # 跳过第一行无效数据
|
|
|
|
|
|
cuisine_column = '菜系'
|
|
|
score_column = '评分'
|
|
|
|
|
|
# 计算各菜系的评分平均分
|
|
|
cuisine_scores = df.groupby(cuisine_column)[score_column].mean().reset_index()
|
|
|
cuisine_scores.columns = ['菜系', '平均分']
|
|
|
|
|
|
# 使用Pyecharts绘制折线图
|
|
|
line = Line()
|
|
|
line.add_xaxis(cuisine_scores['菜系'].tolist())
|
|
|
line.add_yaxis("平均分", cuisine_scores['平均分'].tolist())
|
|
|
|
|
|
# 设置图表标题、工具提示等
|
|
|
line.set_global_opts(
|
|
|
title_opts=opts.TitleOpts(title="各菜系评分平均分折线图"),
|
|
|
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
|
|
|
xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
|
|
|
yaxis_opts=opts.AxisOpts(type_="value")
|
|
|
)
|
|
|
|
|
|
# 渲染图表到HTML文件
|
|
|
line.render('cuisine_scores_percent.html')
|
|
|
|
|
|
# 计算各菜系的消费次数之和(即评论总数之和)
|
|
|
cuisine_counts = df.groupby('菜系')['评论总数'].sum().reset_index()
|
|
|
cuisine_counts.columns = ['菜系', '消费次数']
|
|
|
|
|
|
# 使用Pyecharts绘制饼图
|
|
|
pie = (
|
|
|
Pie()
|
|
|
.add(
|
|
|
"",
|
|
|
[list(z) for z in zip(cuisine_counts['菜系'], cuisine_counts['消费次数'])],
|
|
|
radius=["40%", "75%"], # 设置饼图的内外半径
|
|
|
label_opts=opts.LabelOpts(formatter="{b}: {c} ({d}%)"), # 标签格式,包括菜系名称、消费次数和百分比
|
|
|
)
|
|
|
.set_global_opts(title_opts=opts.TitleOpts(title="各菜系消费次数占比"))
|
|
|
# 不需要在这里再设置 label_opts,因为它已经在 add 方法中设置过了
|
|
|
)
|
|
|
|
|
|
# 渲染图表到HTML文件
|
|
|
pie.render('cuisine_consumption_pie_chart.html')
|
|
|
|
|
|
# 计算每个菜系的人均消费总和和平均消费(假设每个菜系有多条记录)
|
|
|
cuisine_sums = df.groupby('菜系')['人均消费'].sum()
|
|
|
cuisine_counts = df.groupby('菜系')['人均消费'].count()
|
|
|
cuisine_averages = cuisine_sums / cuisine_counts
|
|
|
|
|
|
# 转换为DataFrame以便使用
|
|
|
cuisine_df = cuisine_averages.reset_index()
|
|
|
cuisine_df.columns = ['菜系', '平均消费']
|
|
|
|
|
|
# 绘制柱状图
|
|
|
bar = Bar()
|
|
|
bar.add_xaxis(cuisine_df['菜系'].tolist())
|
|
|
bar.add_yaxis('平均消费', cuisine_df['平均消费'].tolist())
|
|
|
|
|
|
# 设置全局配置项
|
|
|
bar.set_global_opts(
|
|
|
title_opts=opts.TitleOpts(title='各菜系人均消费占比柱状图'),
|
|
|
xaxis_opts=opts.AxisOpts(name='菜系'),
|
|
|
yaxis_opts=opts.AxisOpts(name='平均消费'),
|
|
|
toolbox_opts=opts.ToolboxOpts()
|
|
|
)
|
|
|
|
|
|
# 渲染图表到HTML文件(可以在浏览器中打开查看)
|
|
|
bar.render('cuisine_average_consumption.html') |