前言
暑假是学生老师们拥有的最长假期,这么长的假期,光待在家里太浪费了,何不选择一个好去处,欣赏欣赏夏季的美景?当然,并不是所有地方都适合这个时节去的,那么,暑假适合去国内那些地方旅游呢?现在我们学习了python,当然要用不一样的方式来选择旅游景点,利用大数据的神奇之处,我们一起来分析全国的旅游景点数据,选出最佳旅游景点。
旅游出行数据分析
一、读入数据
1、导入包
import numpy as npimport pandas as pd import os from pyecharts.charts import Bar, Pie, Map, Page, BMap, Scatterfrom pyecharts import options as opts
2、获取文件路径
data_files = os.listdir('E:/py练习/数据分析/旅游网站数据/data/')
3、循环读取
df_all = pd.DataFrame()for file in data_files:df_one = pd.read_excel('E:/py练习/数据分析/旅游网站数据/data/{}'.format(file))df_all = df_all.append(df_one, ignore_index=True)df_all.head()
二、数据清洗和数据预处理
1、复制一份并删除重复值
df_qa = df_all.copy()df_qa.drop_duplicates(inplace=True)df_qa.shape
2、提取省份
df_qa['province'] = df_qa.districts.str.split('·').apply(lambda x:x[0])
3、计算销售额
df_qa['sales_volume'] = df_qa['saleCount'] * df_qa['qunarPrice']df_qa.head()
三、数据可视化
1、各星级景点数量
# 1. 景点星级分布star_num = df_qa['star'].value_counts() # 数据对data_pair = [list(z) for z in zip(star_num.index, star_num.values.tolist())]# 饼图pie1 = Pie(init_opts=opts.InitOpts(width='900px', height='750px'))pie1.add( series_name="num",radius=["35%", "60%"],data_pair=data_pair,label_opts=opts.LabelOpts(position="outside",formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",background_color="#eee",border_color="#aaa",border_width=1,border_radius=4,rich={"a": {"color": "#999", "lineHeight": 22, "align": "center"},"abg": {"backgroundColor": "#e3e3e3","width": "100%","align": "right","height": 22,"borderRadius": [4, 4, 0, 0],},"hr": {"borderColor": "#aaa","width": "100%","borderWidth": 0.5,"height": 0,},"b": {"fontSize": 16, "lineHeight": 33},"per": {"color": "#eee","backgroundColor": "#334455","padding": [2, 4],"borderRadius": 2,},},),)pie1.set_global_opts(legend_opts=opts.LegendOpts(pos_left="left", pos_top='30%', orient="vertical"), title_opts=opts.TitleOpts(title='全国景点星级分布'))pie1.set_series_opts(tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"))pie1.render_notebook()
2、全国各省份景点数量Top15
# 国内城市top10city_top10 = df_qa.province.value_counts()[:15]# 条形图bar1 = Bar(init_opts=opts.InitOpts(width='900px', height='750px'))bar1.add_xaxis(city_top10.index.tolist())bar1.add_yaxis("省份", city_top10.values.tolist())bar1.set_global_opts(title_opts=opts.TitleOpts(title="全国景点数量Top10"),visualmap_opts=opts.VisualMapOpts(max_=1200))bar1.render_notebook()
3、各省份销量热力图
saleCount_num = df_qa.groupby('province')['saleCount'].sum().sort_values(ascending=False) # 地图map1 = Map(init_opts=opts.InitOpts(width='1350px', height='750px'))map1.add("", [list(z) for z in zip(saleCount_num.index.tolist(), saleCount_num.values.tolist())],maptype='china') map1.set_global_opts(title_opts=opts.TitleOpts(title='各省份销量热力图'),visualmap_opts=opts.VisualMapOpts(max_=80000))map1.render_notebook()
4、景点门票销量排行分析Top20
sales_rank = df_qa.pivot_table(index='sightName', values='saleCount', aggfunc='mean')sales_rank = sales_rank.reset_index()sales_rank['saleCount'] = sales_rank['saleCount'].astype('int') sales_rank = sales_rank.sort_values('saleCount', ascending=False)[:20]sales_rank = sales_rank.sort_values('saleCount', ascending=True) bar2 = Bar(init_opts=opts.InitOpts(width='1350px', height='750px'))bar2.add_xaxis(sales_rank.sightName.tolist())bar2.add_yaxis("景点", sales_rank.saleCount.tolist())bar2.set_global_opts(title_opts=opts.TitleOpts(title="景点门票销量排行分析Top20"))bar2.set_series_opts(label_opts=opts.LabelOpts(position='right'))bar2.set_colors(['#D02C2A'])bar2.reversal_axis()bar2.render_notebook()
5、景点门票销售额排行分析Top20
sales_rank_2 = df_qa.pivot_table(index='sightName', values='sales_volume', aggfunc='mean') sales_rank_2 = sales_rank_2.reset_index()sales_rank_2['sales_volume'] = sales_rank_2['sales_volume'].astype('int') sales_rank_2 = sales_rank_2.sort_values('sales_volume', ascending=False)[:20]sales_rank_2 = sales_rank_2.sort_values('sales_volume', ascending=True) bar3 = Bar(init_opts=opts.InitOpts(width='1350px', height='750px'))bar3.add_xaxis(sales_rank_2.sightName.tolist())bar3.add_yaxis("景点", sales_rank_2.sales_volume.tolist())bar3.set_global_opts(title_opts=opts.TitleOpts(title="景点门票销售额排行分析Top20"))bar3.set_series_opts(label_opts=opts.LabelOpts(position='right'))bar2.set_colors(['#D02C2A'])bar3.reversal_axis()bar3.render_notebook()
结语
这里就没有带小伙伴一起从爬旅游网站的数据开始做了,本次爬取的旅游网站数据是去哪儿网的数据,如果还有不会爬虫的小伙伴可以去看我以前的博客python0基础爬虫实战,如果有小伙伴想偷懒,也可以直接私信林哥,林哥可以直接把数据给到大家,大家可以尝试着做一做这个项目,可以尝试不同的风格,不同的色彩,做出来还是挺有成就感的。
喜欢林哥的文章可以给林哥点点关注,一键三连,以后林哥会经常出一些干货来帮助大家学习编程 愿大家都能在编程这条路,越走越远。