Skip to content

Instantly share code, notes, and snippets.

@kboghe
Created September 28, 2020 08:33
Show Gist options
  • Save kboghe/ea5b7f1de1f0df046c207a27597b2b61 to your computer and use it in GitHub Desktop.
Save kboghe/ea5b7f1de1f0df046c207a27597b2b61 to your computer and use it in GitHub Desktop.
peakpopularityhour
##########################
#check popularity by hour#
##########################
byhour_pop = geos_locinfo_poptimes[(geos_locinfo_poptimes['category_aggregated'] == 'restaurant')].groupby(['country', 'day list','hour list'])['percentage busy'].agg(['mean']).reset_index()
byhour_pop = byhour_pop[~byhour_pop['hour list'].isin(['day marked as closed','not enough location data available for this day'])]
byhour_pop = byhour_pop.pivot(index=['country','day list'], columns='hour list', values='mean')
hours_range = [str(x) for x in range(24)]
hours_range_night,hours_range_evening = hours_range[0:2],hours_range[17:24]
hours_evening_night = hours_range_evening + hours_range_night
byhour_pop = byhour_pop[hours_evening_night].reset_index()
byhour_pop["day order"] = byhour_pop["day list"].replace({"monday": "0", "tuesday": "1","wednesday":"2","thursday":"3","friday":"4","saturday":"5","sunday":"6"})
byhour_pop['country order'] = byhour_pop["country"].replace({"NL": "0", "BE": "1","DE":"2","FR":"3","ES":"4","IT":"5"})
byhour_pop = byhour_pop.sort_values(['country order','day order'])
byhour_pop = byhour_pop.drop(['day order','country order'],axis=1).drop(['day list'],axis=1)
peak_hour = geos_locinfo_poptimes[(geos_locinfo_poptimes['category_aggregated'] == 'restaurant') & (geos_locinfo_poptimes['percentage busy'] == geos_locinfo_poptimes.groupby('url')['percentage busy'].transform('max'))]
peak_hour = peak_hour.groupby(['country','hour list'])['url'].count().reset_index()
total = peak_hour.groupby('country').sum().reset_index()
total.columns = ['country','total_places']
peak_hour = pd.merge(peak_hour,total,how="left")
peak_hour['perc'] = peak_hour['url']/peak_hour['total_places'] * 100
peak_hour = peak_hour[['country','hour list','perc']]
peak_hour = peak_hour[peak_hour['hour list'].isin(hours_evening_night)]
peak_hour['hours_adj'] = peak_hour['hour list'].replace({'0':'24','1':'25'})
peak_hour = peak_hour.sort_values(['country','hours_adj'])
perc_described = peak_hour.groupby('country')['perc'].sum()
sns.set_theme(style="darkgrid")
fig, axn = plt.subplots(2, 2)
plt.subplots_adjust(top = 0.82, bottom=0.1, hspace=0.6, wspace=0.35)
fig.suptitle('Peak popularity of restaurants by hour of the day')
for ax,country,title in zip(axn[:,0],['ES','IT'],['Spain','Italy']):
h = sns.heatmap(byhour_pop[byhour_pop.country == country].drop('country',axis=1), ax=ax,cbar=False,cmap='coolwarm',annot=True, xticklabels=['17h','18h','19h','20h','21h','22h','23h','24h','1h'], yticklabels = ['Mon','Tue','Wed','Thur','Fri','Sat','Sun'],square=False)
h.set_xticklabels(['17h','18h','19h','20h','21h','22h','23h','24h','1h'], size=10,rotation=0)
ax.set_title(title,x=1.15, y=1.10,fontweight='bold')
ax.set_xlabel("")
for ax,country,title in zip(axn[:,1],['ES','IT'],['Spain','Italy']):
b = sns.barplot(ax=ax,data=peak_hour[peak_hour.country == country].drop('country',axis=1),y='hour list',x='perc',palette='coolwarm',hue='perc',dodge=False)
b.legend_.remove()
b.set_yticklabels(['17h','18h','19h','20h','21h','22h','23h','24h','1h'], size=10,rotation=0)
ax.set_title("")
ax.set_xlabel('% of restaurants reaching peak popularity',size=9)
ax.set_ylabel('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment