Use Python to analyze the distribution relationship between Luckin and Starbucks nationwide stores

WillLuckinshakeStarbucks’industrystatus?

Lastmonth,LuckinCoffee’ssaucelattebecamepopular,puttingLuckininthespotlightagain.Thelasttimewaswhenitcommittedfinancialfraud.

Thedomesticcoffeemarkethasbeenboominginrecentyears,drivingtherapiddevelopmentofmanycoffeebrandsincludingLuckin.From2013to2023,China’spercapitacoffeeconsumptionisexpectedtoincreaseby238%.Thetotalnumberofcoffeestoresinthecountrynowexceeds100,000.Andthenumberisgrowingbytensofthousandseveryyear.

TheriseofLuckinCoffeeremindsusofStarbucks,thebenchmarkinthecoffeeindustry.Starbuckshasbeenalmostsynonymouswithcoffeeinthepasttenyearsandisalsothelifestyleofurbanwhite-collarworkers.

WhatishappeningnowisthatwhereverthereisaStarbucksstore,thereisalmostaLuckinstorewithinafewhundredmeters,andsomeareevensurroundedbytwoorthree.

ThefollowingusesvisualdashboardsandPythondataanalysistocomparethedifferencesandcorrelationsbetweenStarbucksandLuckinstoresintermsofquantity,regionaldistribution.

Therearetwomainfindings:

1.StarbucksismoreconcentratedineconomicallydevelopedcoastalareassuchastheYangtzeRiverDelta,PearlRiverDelta,andBeijing-Tianjin-Hebei,especiallyfirst-andsecond-tiercities.LuckinismoredispersedthanStarbucksandhasstoresinmanythird-andfourth-tiercitiesandbelow.

2.LuckinstorelocationsareconcentratedaroundStarbucks.Datashowsthatwithinaradiusof500meters,thereareanaverageof0.6LuckinstoresaroundeachStarbucksstorenationwide.

Preparationphase

ThetoolsusedinthisanalysistaskincludeNextDataRobot,Python,andshapely.

XiamiaoDataRobotisaclouddataplatformthatintegratesdatasets,datacleaning,dataanalysis,datavisualizationandbillboardconstruction.TheStarbucksandLuckinstoredatasetsusedinthisanalysisareallstoredinXiamiaoDataRobot.

Wewilldevelopdatadashboardsbasedondatasets,andalsouseAPIinterfacestodirectlycallPythonfordataanalysisandvisualization.Pythonisusedtoconnecttothedatainterfaceoftherobotandprocessandanalyzethedata.

Shapelyisathird-partylibraryforPython,usedtoprocesslatitudeandlongitudedata,andcandeterminethedistanceandinclusionrelationshipbetweendifferentgeographicalcoordinates.

Dataset

BecauseweneedtocompareandanalyzethenumberandlocationofStarbucksandLuckinstores,themainfieldsofthedatasetincludestorename,longitude,latitude,andcity.

?

Note:Thedatasetisin2022,andthereisaquantitativeerrorofabout20%.

?

NationalStarbuckscoffeestoredataset:NationalLuckinCoffeeStoredataset:

Bothdatasetsarestoredonthenextdatarobotplatform.Youcandirectlyviewandusethedatasetsthroughthedataview,whichwewillusetobuildadatadashboardlater.

BecausePythonisneededtoprocessthedatalater,thedataneedstobeobtainedthroughtheAPIdatainterface.Itisveryconvenienttooperateandcanbesavedforlateruse.

importrequests
headers={"x-token":"yourauthenticationtoken"}
response=requests.get("http://app.chafer.nexadata.cn/openapi/v1/sheet/sht22nId5uouP2/records?size=1&page=1",headers=headers)
print(response.json())

Buildananalysisdashboard

ItisrelativelysimpletobuildadashboardontheNextDataRobot.First,createaprocesstaskandselecttwodataviews:StarbucksandLuckin.

ThencreateaKanbanboardandeditthedesignchart,whichissimilartowhatweusuallydoonBIsoftware.

Therearemorethanadozenchartformshere,whichcanbasicallymeetmostvisualizationscenarios.

ComparisonofthenumberofStarbucksandLuckinstoresnationwide

Asofthedatasettime(2022),thenumberofStarbucksstoresnationwideisexpectedtobe4,442,andthenumberofLuckinCoffeestoresnationwideisexpectedtobe3,904.Starbuckshas14%morestoresthanLuckin.

Judgingfromthemagnitude,thetwoareveryclose,andLuckinisexpandingitsstoresataterrifyinggrowthrate.Takingthebusinessdistrictnearmyhomeasanexample,therewasonlyoneLuckinlastyear,andtherearethreethisyear.

StarbucksismoredemandingthanRuixingintermsoflocation,storeopeningcost,storearea,andnumberofstoreemployees.Ruixingspecializesintakeout+takeout.ThisisalsothereasonwhyRuixingcanexpandrapidlyinadditiontomarketdemandfactors.

Starbucksisdistributedinthetop20citiesacrossthecountry

ThetopfivecitieswiththenumberofStarbucksstoresare:Shanghai,Beijing,Hangzhou,Shenzhen,andGuangzhou.Amongthetop20cities,thereare6intheYangtzeRiverDelta,5inthePearlRiverDelta,and2intheBeijing-Tianjin-Hebeiregion.

ThenumberofStarbucksstoresinShanghaiis668,whichistwiceasmanyasthesecond-placeBeijing.Atthesametime,ShanghaiisalsothecitywiththelargestnumberofStarbucksstoresintheworld.ItseemsthatthepeopleofShanghaihaveawell-deservedloveforcoffee.

ThenumberofStarbucksinHangzhouissecondonlytoShanghaiandBeijing,andhigherthanShenzhenandGuangzhou.Internetande-commercepractitionersinHangzhoualsopreferStarbucks.

LuckinisdistributedinTop20citiesacrossthecountry

ThetopfivecitieswiththenumberofLuckinstoresare:Shanghai,Beijing,Guangzhou,Shenzhen,andHangzhou.TheyarethesameasthetopfivecitiesforStarbucks,buttheorderingisslightlydifferent.

Amongthetop20cities,thereare6intheYangtzeRiverDelta,2inthePearlRiverDelta,and2intheBeijing-Tianjin-Hebeiregion.

Starbucksismainlyconcentratedinfirst-andsecond-tiercoastalcities,whileLuckinisrapidlyoccupyingthemarketininlandcities.Luckin’stop20citiesalreadyincludeHefei,Kunming,andZhengzhou,butthesethreeprovincialcapitalcitiesarenotinStarbucks’top20.

Therefore,thedistributionofLuckinstoresismoredispersedandnotoverlyconcentratedinfirst-tiercities.

Starbucksnationwidedistributionheatmap

ItcanalsobeseenfromtheheatmapofStarbucksstoresthattheredhigh-densityareasaremainlyconcentratedincoastalareas,whiletheinlandareasaredistributedinapoint-likemannerandarerelativelysparse.

Ruixing’snationwidedistributionheatmap

ThedistributionofLuckinstoresismoreeven.Inadditiontocoastalareas,therearealsomanystoresincentralChinasuchasHunan,Anhui,Hubei,andHunan.

StarbucksShanghaidistributionheatmap

Shanghaiisthecitywiththelargestcoffeeconsumptiondemandinthecountry.Let’stakealookatthedistributionofStarbucksstoresinShanghai.

Generallyspeaking,StarbucksstoresareconcentratedintheinnerringofShanghai,andarescatteredoutwardsindottedlines.Thefivemajornewcitiesinthesuburbs,PudongAirport,andHongqiaohubarealsorelativelyconcentratedareas.

LuckinShanghaidistributionheatmap

TheconcentrationofLuckinintheinnerringofShanghaiisnotasobviousasthatofStarbucks,andtheoverallnumberisalsomuchsmaller.

Pythondataanalysis

Previously,weanalyzedthedistributionofStarbucksandLuckinstoresacrossthecountrybybuildingavisualsignageontheNextMiaoRobot.Theregionaldifferencesarestillquiteobvious.

Let’sfurtheranalyzetherelationshipbetweenStarbucksandLuckinstores.WeknowthatLuckinCoffeeisarisingstar.ItissaidthatthelocationofmanystoresmainlydependsonwhetherthereisaStarbucksnearby.

So,onaverage,howmanyLuckinstoresaretherearoundeachStarbucksstorenationwide?Thistime,lookatthegatheringsituationofLuckinaroundStarbucksfroma500-meterradius.

WeusePythonanditsthird-partylibraryshapelytoprocessdata.Shapelyismainlyusedtoprocessgeographicalcoordinatedata.

Step1:Importtherequiredlibraries

#Importrelatedlibraries
importpandasaspd
importrequests
importtime
fromshapely.geometryimportPoint
fromshapely.geometry.polygonimportPolygon

Step2:ExtractdatafromtheAPI

#ExtractStarbucksandLuckinstoredataandcallitthroughthenextrobotAPIcall
#ExtractStarbucksstoredata
headers={"x-token":"tk7a2980431688455e8976e4bad4d13d6a"}
starbucks_list=[]
foriinrange(1,10):
response_1=requests.get("http://app.chafer.nexadata.cn/openapi/v1/sheet/sht22nId5uouP2/records?size=500&page={0}".format(i),headers=headers)
starbucks=response_1.json()['data']['list']
starbucks=pd.DataFrame(starbucks)
time.sleep(1)
starbucks_list.append(starbucks)
starbucks=pd.concat(starbucks_list)
#ExtractLuckinstoredata
luckin_list=[]
forjinrange(1,9):
response_2=requests.get("http://app.chafer.nexadata.cn/openapi/v1/sheet/sht22nIeomVmYy/records?size=500&page={0}".format(j),headers=headers)
luckin=response_2.json()['data']['list']
luckin=pd.DataFrame(luckin)
luckin_list.append(luckin)
time.sleep(1)
luckin=pd.concat(luckin_list)

Step3:DeterminethenumberofLuckinstoreswithina500-meterradiusofaStarbucksstore

#DrawageographicalareawitharadiusofXXmetersbasedonthecoordinatesoftheStarbuckscoffeeshop
defcircle(data,radius):
#radiusrepresentsthearearadius
#Givengeographicalcoordinates
center_latitude=float(data['dimension'])
center_longitude=float(data['longitude'])
#Createacirculararea
center_point=Point(center_longitude,center_latitude)
circle=center_point.buffer(radius/111300)
#Createpolygonarea
polygon=Polygon(circle.exterior)
returnpolygon

#Constructcoordinatepointsbasedonlongitudeandlatitude
defpoint(data):
#Givengeographicalcoordinates
center_latitude=float(data['dimension'])
center_longitude=float(data['longitude'])
#Createcoordinatepoints
center_point=Point(center_longitude,center_latitude)
returncenter_point

#DeterminewhetherLuckinCoffeeShopiswithin500mofStarbucks
defis_inside(data):
polygon=data['Polygon']
#Determinewhetherthecoordinatesarewithinthearea
n=0
luckin_city=luckin[luckin['city']==data['city']]
forpointinluckin_city['Point']:
is_inside=polygon.contains(point)
#Printthejudgmentresult
ifis_inside:
n=n+1
returnn

#Drawageographicalareawitharadiusof500metersbasedonthecoordinatesofStarbucksstores
starbucks['Polygon']=starbucks.apply(circle,axis=1,args=(500,))

#ConstructcoordinatepointsbasedonthelongitudeandlatitudeofLuckinstores
luckin['Point']=luckin.apply(point,axis=1)

#DeterminewhetheraLuckinstoreiswithina500-meterradiusofaStarbucksstore
starbucks['Luckin_numbers']=starbucks.apply(is_inside,axis=1)

Thedataafterprocessingisasfollows:

Step4:Analyzethedata

  1. Withinaradiusof500meters,thereareanaverageof0.6LuckinstoresaroundeachStarbucksstorenationwide.

#Withinaradiusof500meters,thereareanaverageof0.6LuckinstoresaroundeachStarbucksstorenationwide.
starbucks['Luckin_numbers'].mean()

Output:0.6

  1. Withinaradiusof500meters,thereare7LuckinstoressurroundingthelargestnumberofStarbucksstoresinthecountry.

#Withinaradiusof500meters,thereare7LuckinstoresaroundthemostStarbucksstores
starbucks['Luckin_numbers'].max()

Output:7

  1. Withinaradiusof500meters,theaveragenumberofLuckinstoresaroundaStarbucksstoreisrankedbycity.ThelargestnumberisLinyiCity,withanaverageof1.8LuckinstoresaroundeachStarbucksstore.

#RankingofaveragenumberofLuckinstoresineachcitywithina500-meterradiusofStarbucksstores
#Themostisthatthereareanaverageof1.8LuckinstoresaroundeachStarbucksstoreinLinyiCity
city_list=[]
forcityinpd.unique(starbucks['city']):
avg_luckin_numbers=starbucks[starbucks['city']==city]['Luckin_numbers'].mean()
starbucks_nums=starbucks[starbucks['city']==city]['name'].count()
city_list.append([city,starbucks_nums,avg_luckin_numbers])

df=pd.DataFrame(city_list,columns=['city','starbucks_nums','avg_luckin_numbers'])
df.sort_values(by=['avg_luckin_numbers'],axis=0,ascending=False)

Output:

ItseemsthatLuckindoeshaveanindissolublebondwithStarbucks.NowonderweseesomanyLuckinstoresaroundStarbucks.

Starbucksstoreshavedevelopedthehabitofdrinkingcoffeeamongsurroundingusers.Inotherwords,therearemanycoffeedrinkingusershere,soStarbucksjustcameheretoopenastore.ThenopeningLuckinCoffeenearStarbuckscanacquirealargewaveofpotentialusersatalowcost,despitecompetition.,it’sstillveryworthwhile.

Summary

WeusedvisualdashboardsandPythondataanalysistoshowtheregionaldistributionandcorrelationofStarbucksandLuckinCoffeestores.Infact,therearemanythingsworthanalyzingandexploring.

Forexample,insomecitiesthereareveryfeworalmostnoLuckinstoresaroundStarbucksstores.Whatisthereason?Isitapotentialopportunityorapittobeavoided?

Ifyouareinterested,youcangiveitatry.

Reprintedfrom:pythonbigdata

Ourmembershipcommunity

WeChatconsultation:coderv5

Recommendedreading:
GettingStarted:ThemostcomprehensiveproblemoflearningPythonfromscratch|LearnedPythonfor8monthsfromscratch|Practicalprojects|ThisistheshortcuttolearnPython

Essentialinformation:CrawlingDoubanshortreviews,themovie”TheNextUs”|AnalysisofthebestNBAplayersin38years|Fromhighlyanticipatedtowordofmouth!DetectiveTang3isdisappointing|WatchthenewLegendofHeavenandDragonSwordwithlaughter|Thekingoflanternriddleanswers|UsePythontomakeamassivesketchofyoungladies|Mission:Impossibleissopopular,Iusemachinelearningtomakeaminirecommendationsystemformovies

Fun:pinballgame|nine-squaregrid|beautifulflowers|twohundredlinesofPython”TiantianCoolRun”game!

AI:Arobotthatcanwritepoetry|Colorizepictures|Predictincome|Mission:Impossibleissopopular,Iusemachinelearningtomakeaminimovierecommendationsystem

Gadget:ConvertPdftoWord,easilyconverttablesandwatermarks!|Savehtmlwebpagestopdfwithoneclick!|GoodbyePDFextractioncharges!|Use90linesofcodetocreatethemostpowerfulPDFconverter,one-clickconversionofword,PPT,excel,markdown,andhtml|CreateaDingTalklow-priceticketreminder!|60linesofcodemadeavoicewallpaperswitcherthatIcanwatcheveryday!|