码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Pandas+Pyecharts | 上海市餐饮数据分析可视化


    文章目录

    • 🏳️‍🌈 1. 导入模块
    • 🏳️‍🌈 2. Pandas数据处理
      • 2.1 读取数据
      • 2.2 去除最后两行无用的列
      • 2.3 查看索引、数据类型和内存信息
      • 2.4 清除空值
      • 2.5 行政区替换
    • 🏳️‍🌈 3. Pyecharts数据可视化
      • 3.1 上海各类餐饮店数量分布玫瑰图/各区餐饮店数量分布
      • 3.2 各餐饮类型人均消费
      • 3.3 上海各区餐饮店数量地图分布
      • 3.4 各区餐饮口味好评率、环境好评率、服务好评率
      • 3.5 各餐饮类型口味好评率、环境好评率、服务好评率
    • 🏳️‍🌈 4. 在线运行地址
    • 推荐阅读

    大家好,我是 👉【Python当打之年(点击跳转)】

    本期我们通过分析上海市餐饮数据,看看:

    • 哪个区的餐饮店铺分布最多
    • 各类餐饮口碑评价怎么样
    • 各类餐饮的人均消费大概定价多少
    • 各区都适合开哪些类型的店铺
    • …

    希望对你有所帮助,如有疑问或者需要改进的地方可以私信小编。

    涉及到的库:

    • Pandas — 数据处理
    • Pyecharts — 数据可视化
    • Matplotlib — 数据可视化

    可视化部分:

    • 柱状图 — Bar
    • 地图 — Map
    • 饼图 — Pie
    • 组合组件 — Grid

    🏳️‍🌈 1. 导入模块

    import numpy as np
    import pandas as pd
    from collections import Counter
    from pyecharts.charts import Pie
    from pyecharts.charts import Bar
    from pyecharts.charts import Grid
    from pyecharts.charts import Page
    from pyecharts.charts import Map
    from pyecharts import options as opts
    from pyecharts.commons.utils import JsCode
    
    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1.inset_locator import inset_axes
    plt.rcParams['font.sans-serif'] = ['SimHei']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    🏳️‍🌈 2. Pandas数据处理

    2.1 读取数据

    df = pd.read_csv('上海餐饮数据.csv')
    df.head()
    
    • 1
    • 2

    在这里插入图片描述

    2.2 去除最后两行无用的列

    df = df.iloc[:,:-2]
    df.head()
    
    • 1
    • 2

    在这里插入图片描述

    2.3 查看索引、数据类型和内存信息

    df.info()
    
    • 1

    在这里插入图片描述

    一共 96398 条信息,其中类别和行政区这两个字段有数据缺失。

    2.4 清除空值

    df.isnull().sum()
    df = df.dropna()
    
    • 1
    • 2

    2.5 行政区替换

    卢湾区目前已经划入黄浦区,所以将行政区列中的卢湾区替换成黄浦区:

    df['行政区'] = df['行政区'].str.strip()
    df['行政区'] = df['行政区'].replace('卢湾区','黄浦区')
    
    • 1
    • 2

    🏳️‍🌈 3. Pyecharts数据可视化

    3.1 上海各类餐饮店数量分布玫瑰图/各区餐饮店数量分布

    def get_pie(regions, region_count, category, count, mean, size, colors):    
    
        num= len(size)
        width = 2 * np.pi / num
        rad = np.cumsum([width] * num)
        plt.figure(figsize=(8, 8),dpi=500)
        
        ax = plt.subplot(projection='polar')
        ax.set_ylim(-1, np.ceil(max(size) + 1))
        ax.set_theta_zero_location('N',-5.0)
        ax.set_theta_direction(1)
        ax.grid(False)
        ax.spines['polar'].set_visible(False)
        ax.set_yticks([])
        ax.set_thetagrids([])
        
        ax.bar(rad, size, width=width, color=colors, alpha=1)
        ax.bar(rad, 1, width=width, color='white', alpha=0.15)
        ax.bar(rad, 3, width=width, color='white', alpha=0.1)
        ax.bar(rad, 5, width=width, color='white', alpha=0.05)
        ax.bar(rad, 7, width=width, color='white', alpha=0.03)
    
        # 设置text
        for i in np.arange(num):
            if i < 8:
                ax.text(rad[i], size[i]-0.2, f'{category[i]}\n{count[i]}家\n({mean[i]})', rotation=rad[i] * 180 / np.pi -5,
                        rotation_mode='anchor', fontstyle='normal', fontweight='black', color='white', size=size[i]/2.2,
                        ha="center", va="top" )
            elif i < 15:
                ax.text(rad[i]+0.02, size[i]-0.7, f'{category[i]}\n{count[i]}家\n({mean[i]})', fontstyle='normal',
                        fontweight='black', color='white', size=size[i] / 1.6, ha="center" )
            else:
                ax.text(rad[i], size[i]+0.1, f'{category[i]} {count[i]}家 ({mean[i]})', rotation=rad[i] * 180 / np.pi + 85,
                        rotation_mode='anchor', fontstyle='normal', fontweight='black', color='black', size=4,
                        ha="left", va="bottom" )
    
        
        cell_width = 20  
        cell_height = 5
        axins1=inset_axes(ax,width=3.05, height=cell_height-0.6, loc=1)
        regions_len = len(regions)
        ncols = 2
        nrows = regions_len // ncols + int(regions_len % ncols > 0)
        axins1.set_xlim(0,cell_width*3.5)
        axins1.set_ylim(cell_height* (nrows-0.5),1-cell_height/2.)
        axins1.yaxis.set_visible(False)
        axins1.xaxis.set_visible(False)
        axins1.set_axis_off()
        
        for i in range(regions_len):
            row = i % nrows
            col = i // nrows
            y = row*2
            text_pos_x = cell_width * col + 10
            axins1.text(text_pos_x-5, y, str(region_count[i]), fontsize=4,color='#FAFAFA',fontweight='bold',
                        horizontalalignment='left',verticalalignment='center',backgroundcolor="#EC407A"
                       )
            axins1.text(text_pos_x, y, regions[i], fontsize=4,color='#3F51B5',fontweight='bold',
                        horizontalalignment='left',verticalalignment='center'
                       )
        plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在这里插入图片描述

    • 店铺较多的餐饮为甜点、快餐、咖啡厅、西餐等,紧随其后是浙菜、川菜、火锅、烧烤等类型

    3.2 各餐饮类型人均消费

    bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='1000px',bg_color='#0d0735'))
        .add_xaxis(df_category_mean_tmp['类别'].tolist())
        .add_yaxis("", df_category_mean_tmp['人均消费'].tolist())
        .set_series_opts(label_opts=opts.LabelOpts(position="right",
                                                  font_size=12,
                                                  font_weight='bold',
                                                  formatter='{c} 元'),
                        )
        .set_global_opts(
                        xaxis_opts=opts.AxisOpts(is_show=False,),
                        yaxis_opts=opts.AxisOpts(
                            axislabel_opts=opts.LabelOpts(font_size=13,color='#fff200'),
                            axistick_opts=opts.AxisTickOpts(is_show=False),
                            axisline_opts=opts.AxisLineOpts(is_show=False)
                        ),
                        title_opts=opts.TitleOpts(title="各餐饮类型人均消费",pos_left='center',pos_top='1%',
                                  title_textstyle_opts=opts.TextStyleOpts(font_size=22,color="#38d9a9")),
                        visualmap_opts=opts.VisualMapOpts(is_show=False,
                                          min_=5,
                                          max_=140,
                                          is_piecewise=False,
                                          dimension=0,),
                        )
        .reversal_axis()
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    • 甜点、快餐的人均售价大概在15-20元之间,这也符合上海快节奏的城市生活
    • 店铺数量较多餐饮类型的人均消费基本都在100以内

    3.3 上海各区餐饮店数量地图分布

    地图:

    m0 = (
            Map(init_opts=opts.InitOpts(width='1000px', height='800px',bg_color='#0d0735'))
            .add('',
                 [list(z) for z in zip(regions, region_count)],
                 '上海',
                 is_map_symbol_show=False,
                )
            .set_global_opts(
                title_opts=opts.TitleOpts(
                      title="上海各区餐饮店数量分布", pos_left="center", pos_top='1%',
                      subtitle='<制图@公众号:Python当打之年>', item_gap=5,
                      title_textstyle_opts=opts.TextStyleOpts(
                          font_size=24, font_weight="bold",
                          color="#DC143C",
                      ),
                      subtitle_textstyle_opts=opts.TextStyleOpts(color="#94d82d", font_size=14, font_weight="bold")
                ),
                visualmap_opts=opts.VisualMapOpts(
                    max_= 13000,
                    is_piecewise=True,
                    is_show=True,
                    split_number = 10,
                    pos_top='40%',
                    pos_left='10%',
                ),
            )
        )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    柱状图:

    bar = (
        Bar(init_opts=opts.InitOpts(theme='dark', width='1000px', height='300px',bg_color='#0d0735' ))
        .add_xaxis(regions)
        .add_yaxis("", region_count)
        .set_series_opts(label_opts=opts.LabelOpts(position="insideBottom",
                                                  font_size=10,
                                                  rotate='90',
                                                  vertical_align='middle',
                                                  horizontal_align='left',
                                                  font_weight='bold',
                                                  color='#e7298a',
                                                  formatter='{b}: {c} 家'),
                        markline_opts=opts.MarkLineOpts(
                            data=[opts.MarkLineItem(name="平均:",
                                                    type_ = 'average',)],
                            label_opts=opts.LabelOpts(is_show=False))
        )
        .set_global_opts(
            xaxis_opts=opts.AxisOpts(is_show=False, boundary_gap=False),
            yaxis_opts=opts.AxisOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(
                is_piecewise=True,
                split_number = 10,
                is_show=False,
                max_=13000,),
            title_opts=opts.TitleOpts(
                title=f"各区平均店铺:{df_region['数量'].mean().round(2)}家",
                pos_right="10%",
                pos_top='45%',
                title_textstyle_opts=opts.TextStyleOpts(font_size=13)
            ),
            tooltip_opts=opts.TooltipOpts(
                is_show=True),
        )
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    在这里插入图片描述

    • 上海市各区中浦东新区的餐饮店铺数量最多,超过12000家

    • 闵行区、黄浦区、徐汇区、宝山区、松江区、嘉定区、长宁区、杨浦区、普陀区的餐饮店铺数量也在5000家以上

    3.4 各区餐饮口味好评率、环境好评率、服务好评率

    在这里插入图片描述

    注:好评为评分8.0分以上

    • 静安区、黄浦区、长宁区、徐汇区等几个区的店铺口味好评率、环境好评率、服务好评率都在20%以上

    3.5 各餐饮类型口味好评率、环境好评率、服务好评率

    在这里插入图片描述

    • 南菜、素菜、本菜、亚菜、火锅等几个餐饮类别口味好评率、环境好评率、服务好评率普遍较高

    🏳️‍🌈 4. 在线运行地址

    篇幅原因,部分代码未展示,在线运行地址(含源码): 【点我跳转】


    文章首发:微信公众号 “Python当打之年” ,Python编程技巧推送,希望大家可以喜欢。

    以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享(注明出处)让更多人知道。

    推荐阅读

    • 👨‍🎓 【Pandas+Pyecharts | 山东省高考考生数据分析可视化】
    • 🚗 【Pandas+Pyecharts | 40000+汽车之家数据分析可视化】
    • 🛒 【Pandas+Pyecharts | 20000+天猫订单数据可视化】
    • 🏠 【Pandas+Pyecharts | 广州市已成交房源信息数据可视化】
    • 🏫 【Pandas+Pyecharts | 考研信息数据可视化】
    • 💸 【Pandas+Pyecharts | 某平台招聘信息数据可视化】
    • 🏥 【Pandas+Pyecharts | 医院药品销售数据可视化】
    • 💰️ 【Pyecharts | 比特币每日价格动态可视化】
    • 🥮 【可视化 | Python中秋月饼销量分析,这些口味才是yyds!】
    • 🎦 【Pyecharts | 《白蛇2:青蛇劫起》20000+数据分析可视化】
    • 💑 【Pyecharts | 历年全国各地民政局登记数据分析+可视化】
    • 🌏 【Pandas+Pyecharts | 全国热门旅游景点数据分析+可视化】
    • 🏆 【Pandas+Pyecharts | 2020东京奥运会奖牌数据可视化】
    • 🏹 【Pandas常用基础操作指令汇总 】
    • 🎯 【pandas + pyecharts | ADX游戏广告投放渠道综合分析】
    • 🕔 【Schedule + Pyecharts | 时间序列图(动态轮播图)】
    • 🏠 【Pandas+Pyecharts | 北京某平台二手房数据分析+可视化】
    • 🏫 【Pandas+Pyecharts | 2021中国大学综合排名分析+可视化】
    • 🎦 【爬虫 | Python爬取豆瓣电影Top250 + 数据可视化】
    • 📡 【技巧 | Python创建自己的高匿代理IP池】

  • 相关阅读:
    JUC-内存
    springboot+vue企业废物回收员工任务管理系统java
    数字时代安全风险防范与保密科技创新
    python安全工具开发笔记(三)——python 多线程
    详解SpringBoot的核心特性
    【数值优化之凸集】
    (矩阵) 289. 生命游戏 ——【Leetcode每日一题】
    帮公司面试了一个33岁的程序员,只因这一个细节,被我一眼看穿是培训班出来的,没啥工作经验...
    VoLTE基础自学系列 | VoLTE呼叫流程之VoLTE及PSTN
    基于linux开发的实现类似unix的文件系统
  • 原文地址:https://blog.csdn.net/weixin_42152811/article/details/125716510
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号