任务二:多个饼图呈现各省份不同等级住宿场所占比
企业消费平台为了更好地发展企业业务,向企业客户推荐符合其定位的协议住宿场所,需要分析上题中Top5省份的“三星级/舒适”、“四星级/高档”和“五星级/豪华”住宿场所以及“其它类别”住宿场所(除上述三类外,其余类型住宿场所均归为“其它类别”)的占比情况。请根据指定表中数据,以指定图例进行呈现。
详细描述:
请根据数据库中相关数据集中省份、星级等相关字段统计各省份不同等级住宿场所的占比,绘制多个饼图。
具体任务要求:
1) 根据表格相关字段分别统计上题Top5省份不同星级住宿场所的占比,打印输出各省份名称以及各星级住宿场所的占比情况;
打印语句格式如下:
==省份:A=舒适型住宿占比:**===
==省份:A=高档型住宿占比:**===
==省份:A=豪华型住宿占比:**===
==省份:B=舒适型住宿占比:**===
==省份:B=高档型住宿占比:**===
==省份:B=豪华型住宿占比:**===
……
2) 使用Flask框架,结合Echarts在组合图中绘制多个饼图,每个省份各星级住宿场所占比情况分别用一个饼图进行呈现,将可视化结果截图并保存。
1)ralateLevels.py
import datetime
import numpy as np
import pandas as pd
from sqlalchemy import create_engine
mysql = create_engine('mysql+mysqlconnector://root:123456@127.0.0.1:3306/hotel')
df_orderNum = pd.read_csv(
'E://Python作业//keshihua//enterpriseCustomize//task2//csv//2022-06-22_1655885919.934801df_data.csv')
sql = 'select province,star from platform_rate'
df_platformRate = pd.read_sql(sql, mysql)
df_starNum = pd.DataFrame({'starNum': []})
topFiveProvince = []
for i in range(5):
topFiveProvince.append(df_orderNum['province'][i])
def change_name(x):
if x == '三星级':
return '舒适型'
elif x == '四星级':
return '高档型'
elif x == '五星级':
return '豪华型'
elif x == '舒适型':
return '舒适型'
elif x == '高档型':
return '高档型'
elif x == '豪华型':
return '豪华型'
else:
return '其它类别'
df_platformRate['star'] = df_platformRate['star'].apply(change_name)
df_topFiveProvince = pd.DataFrame({'province': [], 'star': []})
for i in topFiveProvince:
for j in df_platformRate.groupby(df_platformRate['province']):
if j[0] == i:
df_topFiveProvince = pd.concat([df_topFiveProvince, j[1]])
df_topFiveProvince_1 = df_topFiveProvince.groupby([df_topFiveProvince['star'], df_topFiveProvince['province']])
df_topFiveProvince_2 = df_topFiveProvince.groupby( df_topFiveProvince['province'])
count = []
sum_count = []
level = []
province = []
for i in df_topFiveProvince_2:
sum_count.append(i[1]['province'].count())
for i in df_topFiveProvince_1:
count.append(i[1].count())
level.append(i[0][0])
province.append(i[0][1])
print("==省份:" + i[0][1] + "=" + i[0][0] + "住宿占比:**===")
print(count)
print(level)
print(province)
print(sum_count)
df_output = pd.DataFrame({'province': province, 'star': level, 'starNum': count})
print(df_output)
Now_Time = datetime.datetime.now()
filepath = "csv//" + str(Now_Time.date()) + "_" + str(Now_Time.timestamp()) + "df_ralateLevels.csv"
df_output.to_csv(filepath, index=False, header=['province', 'star', 'starNum'])
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/270279.html