<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          你知道全國有多少家海底撈嗎?Python讓你一目了然

          共 4546字,需瀏覽 10分鐘

           ·

          2021-11-27 10:01

          大家好,歡迎來到 Crossin的編程教室 !

          前段時間去公司樓下吃了頓海底撈,排隊人不多。后來沒多久看到新聞?wù)f海底撈要關(guān)閉300家店,300家店是什么概念?海底撈全國一共又有多少家門店?都分布在哪些城市?

          不如我們拿Python來直觀看一看吧!

          海底撈門店分布

          目錄:

          • 1. 數(shù)據(jù)采集

          • 2. 地圖繪制

            • 2.1. 坐標轉(zhuǎn)換

            • 2.2. 地圖繪制

          • 3. 店面分布


          1. 數(shù)據(jù)采集

          直接打開海底撈官網(wǎng) haidilao.com,拖到首頁最下面我們可以看到全球各國家與地區(qū)門店選擇,點擊中國即可進入到中國門店列表,在開發(fā)者模式找到源數(shù)據(jù)地址。

          可以看到請求到的數(shù)據(jù)格式很整潔,我們可以非常方便的處理,看代碼:

          import?requests
          import?pandas?as?pd

          #?接口地址
          url?=?'https://www.haidilao.com/eportal/store/listObjByPosition?country=CN&language=zh'
          #?請求頭
          headers?=?{
          ????"User-Agent":?"Mozilla/5.0?(Windows?NT?10.0;?Win64;?x64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/83.0.4103.97?Safari/537.36",
          ????"accept":?"application/json,?text/plain,?*/*"
          ????}
          r?=?requests.get(url,?headers=headers)
          #?門店數(shù)據(jù)信息
          data?=?r.json()
          #?轉(zhuǎn)化為DataFrame類型
          df?=?pd.DataFrame(data['value'])
          #?存儲本地
          df.to_excel('海底撈.xlsx',index=False)

          數(shù)據(jù)預(yù)覽:

          一共1404家門店

          2. 地圖繪制

          在采集到的數(shù)據(jù)中我們可以看到有經(jīng)緯度坐標(百度地圖),這里我的目標是將店地址繪制到地圖上,采用的是folium繪圖庫。由于這個庫沒有百度地圖,有高德地圖,因此我需要先將經(jīng)緯度坐標從百度轉(zhuǎn)為高德,再進行地圖繪制。

          2.1. 坐標轉(zhuǎn)換

          在高迪開平找到坐標轉(zhuǎn)換的接口,然后直接轉(zhuǎn)換即可。

          https://lbs.amap.com/api/webservice/guide/api/convert

          #?用于存儲轉(zhuǎn)換后的坐標列表
          ss1?=?[]

          for?i?,?location?in?enumerate(df[['longitude','latitude']].values):
          ????location?=?str(location[0])+','+str(location[1])
          ????url?=?'https://restapi.amap.com/v3/assistant/coordinate/convert?'
          ????
          ????parames?=?{
          ????????'locations':location,
          ????????'coordsys':'baidu',
          ????????'key':'你的key',
          ????????}
          ????
          ????r?=?eval(requests.get(url,?params=parames).json()['locations'])
          ????ss?=?[r[1],r[0]]
          ????ss1.append(ss)
          ????print(f'\r{i+1}',end='')

          輸出結(jié)果:

          2.2. 地圖繪制

          基于經(jīng)緯度坐標點+店鋪名稱數(shù)據(jù)用于繪制,代碼如下:

          from?folium?import?plugins
          import?folium

          m?=?folium.Map([39.904989,?116.405285],?#?北京中心區(qū)域經(jīng)緯度
          ????zoom_start=10,
          ????????????????tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}',?#?高德街道圖
          ????????????????#?tiles='https://mt.google.com/vt/lyrs=h&x={x}&y={y}&z={z}',?#?google?地圖
          ????????????????#?tiles='Stamen?Toner',
          ???????????????attr='高德')??

          #?繪制店標記
          groups?=?folium.FeatureGroup('')
          for?l,label?in?zip(ss1,df['storeName'].to_list()):
          ????groups.add_child(
          ????????folium.CircleMarker(
          ????????????location=l,
          ????????????radius=7,
          ????????????color='yellow',
          ????????????fill=True,
          ????????????fill_color='red',
          ????????????fill_opacity=0.4,
          ????????????popup=folium.Popup(html=label,max_width=100),
          ????????)
          ????)
          m.add_child(groups)
          m.add_child(folium.LatLngPopup())??

          #?區(qū)域密度
          marker_cluster?=?plugins.MarkerCluster().add_to(m)??????
          for?l,label?in?zip(ss1,df['storeName'].to_list()):
          ????#?添加marker到地圖
          ????folium.Marker(
          ????????location?=?l,
          ????????icon=None,
          ????????#?popup=folium.Popup(html=label,max_width=100),
          ????).add_to(marker_cluster)

          m.add_child(marker_cluster)

          m.save('海底撈.html')??#?將結(jié)果以HTML形式保存到本地

          輸出結(jié)果對比:

          左側(cè)是繪制的,右側(cè)是官網(wǎng)的

          對比

          3. 店面分布

          同樣的,在高德地圖開放平臺也有經(jīng)緯度坐標轉(zhuǎn)化位置信息的接口,我們通過店鋪經(jīng)緯度信息找到其所在省市(原始數(shù)據(jù)中地址信息格式不統(tǒng)一,比較難解析)。

          citys?=?[]
          provinces?=?[]
          for?i?,?location?in?enumerate(df[['longitude','latitude']].values):
          ????location?=?str(location[0])+','+str(location[1])
          ????url?=?'https://restapi.amap.com/v3/geocode/regeo?'
          ????params?=?{
          ????????'location':location,
          ????????'key':'4e3f878e4f74ad3b522d10830e54cfa2',
          ????????'extensions':'base',
          ????????'batch':'false',
          ????????'roadlevel':0,
          ????????}
          ????
          ????r?=?requests.get(url,?params=params)
          ????data?=?r.json()['regeocode']
          ????city?=?data['addressComponent']['city']
          ????province?=?data['addressComponent']['province']
          ????if?len(city)==0:
          ????????city?=?province
          ????citys.append(city)
          ????provinces.append(province)
          ????print(f'\r{i+1}',end='')
          df['城市']?=?citys

          獲取城市信息后,就可以直接分組統(tǒng)計不同城市的海底撈店鋪數(shù)了。

          df.groupby('城市')['storeId'].nunique().sort_values(ascending=False).head(10)
          城市店鋪數(shù)
          上海市81
          北京市78
          深圳市52
          西安市52
          廣州市47
          武漢市42
          鄭州市41
          杭州市40
          成都市33
          南京市32

          海底撈在全國242個城市有店面,其中在營店面數(shù)最多的城市是上海共81家店,其次是北京有78家店。

          from?pyecharts?import?options?as?opts
          from?pyecharts.charts?import?Map
          from?pyecharts.faker?import?Faker
          from?pyecharts.globals?import?CurrentConfig,?NotebookType
          CurrentConfig.NOTEBOOK_TYPE?=?NotebookType.JUPYTER_LAB

          df?=?pd.read_clipboard()
          c?=?(
          ????Map()
          ????.add("海底撈店面數(shù)",?[list(z)?for?z?in?zip(df.省,?df.數(shù)量)],?"china")
          ????.set_global_opts(
          ????????title_opts=opts.TitleOpts(title="海底撈全國各省店面數(shù)分布"),
          ????????visualmap_opts=opts.VisualMapOpts(max_=200),
          ????)
          )
          c.load_javascript()
          c.render_notebook()
          數(shù)量
          廣東185
          江蘇148
          浙江120
          上海81
          北京78
          山東78
          河南75
          陜西71
          湖北64
          福建59
          安徽55
          河北49
          四川48
          湖南44
          天津30
          江西27
          廣西24
          遼寧22
          重慶22
          山西18
          云南16
          甘肅15
          內(nèi)蒙古15
          吉林14
          海南11
          貴州11
          黑龍江10
          寧夏9
          青海5

          也可以用ExcelEasyShu插件進行繪制,效果如下:

          以上就是我們對海底撈門店分布進行的可視化處理。這種方法在地點相關(guān)的數(shù)據(jù)可視化分析中非常常用,比如停車場分布,房源分布等,感興趣的小伙伴可以找一些類似的數(shù)據(jù)進行練習。

          如果文章對你有幫助,歡迎轉(zhuǎn)發(fā)/點贊/收藏~

          作者:道才

          來源:可以叫我才哥


          _往期文章推薦_

          爬取上海3601個停車場位置,并進行可視化展示




          如需了解付費精品課程教學答疑服務(wù)
          請在Crossin的編程教室內(nèi)回復(fù): 666

          瀏覽 62
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一级A黄色片 | 乱伦综合网 | 操操福利| 五月天乱伦小说 | 男人操女人免费网站 |