Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import altair as alt | |
from vega_datasets import data | |
st.set_page_config(layout="wide") | |
st.markdown('# Lodging') | |
st.markdown(""" | |
A look into the amount of hotels/number of beds available in countries before and after the pandemic to determine how limited the lodging | |
capacities are in a certain country. | |
""") | |
df = pd.read_excel('./data/unwto-tourism-industries-data.xlsx', usecols = 'A,B,E:AE') | |
df_coords = pd.read_csv('./data/GoogleDevCountryGeoCoords.csv') | |
df.rename(columns={'Basic data and indicators':'Country','Unnamed: 1':'Statistics'}, inplace=True) | |
alt.data_transformers.disable_max_rows() | |
#%% | |
for i in range(0, len(df)-1, 8): | |
for j in range(1,8): | |
df.loc[i+j,'Country'] = df.loc[i,'Country'] | |
df['Country'] = df['Country'].str.title() | |
df['Country_key'] = df['Country'].str.lower() | |
df_coords['Country_key2'] = df_coords['name'].str.lower() | |
df_merged = df.merge(right = df_coords, how='left', left_on = 'Country_key', right_on = 'Country_key2') | |
df_merged.tail(20) | |
#%% | |
df_merged['Country'] = df_merged['name'] #Change country names to appropriate format | |
df_merged.drop(columns=['Country_key','Country_key2','country','name'], inplace=True) | |
df_bed_places_coords = df_merged[df_merged['Statistics'] == 'Number of bed-places'].copy() | |
df_bed_places = df_merged[df_merged['Statistics'] == 'Number of bed-places'].copy() | |
df_bed_places.drop(columns=['latitude','longitude'], inplace=True) | |
df_bed_places.drop(columns='Statistics', inplace=True) | |
df_bed_places.dropna(inplace=True) | |
df_bed_places = pd.melt(df_bed_places.loc[:,:], id_vars='Country', var_name='Year',value_name='Number of bed-places') | |
# df_bed_places['Number of bed-places'] = df_bed_places['Number of bed-places'].replace('..', '0') | |
df_bed_places_coords = df_merged[df_merged['Statistics'] == 'Number of bed-places'] | |
df_bed_places_coords.drop(columns='Statistics', inplace=True) | |
df_bed_places_coords = pd.melt(df_bed_places_coords.loc[:,:], id_vars=['Country','latitude','longitude'], var_name='Year',value_name='Number of bed-places') | |
df_bed_places_coords.dropna(inplace=True) | |
df_bed_places_coords['dataAvailable'] = (df_bed_places_coords['Number of bed-places'] != '..') | |
#%% | |
countries = list(df_bed_places['Country'].unique()) | |
country_checkbox = alt.binding_select(options=countries) | |
country_selector = alt.selection_single( | |
fields=['Country'], | |
init = {'Country':countries[1]}, | |
bind = country_checkbox, | |
name='Country' | |
) | |
mouseSelection = alt.selection_single(encodings = ['x'], nearest=True, on='mouseover', empty='none') | |
opacityCondition = alt.condition(mouseSelection, alt.value(1), alt.value(0)) | |
click_selector = alt.selection_multi(fields=['Country']) | |
# click_selector = alt.selection_interval() | |
bedPlaceChart = alt.Chart(df_bed_places).mark_line().encode( | |
x = alt.X('Year:O'), | |
y = alt.Y('Number of bed-places:Q'), | |
color = alt.Color('Country:N'), | |
).transform_filter( | |
country_selector | click_selector | |
).add_selection( | |
country_selector, | |
click_selector | |
).properties( | |
width=600, | |
height=400 | |
) | |
interactionDots = alt.Chart(df_bed_places).mark_point(size=90).encode( | |
x = alt.X('Year:O'), | |
y = alt.Y('Number of bed-places:Q'), | |
color = alt.Color('Country:N'), | |
opacity = opacityCondition | |
).transform_filter( | |
country_selector | click_selector | |
) | |
verticalLine = alt.Chart(df_bed_places).mark_rule(size=2, color='black', strokeDash=[15,15]).encode( | |
x = alt.X('Year:O'), | |
y = alt.Y('Number of bed-places:Q'), | |
opacity=opacityCondition | |
).transform_filter( | |
country_selector | click_selector | |
).add_selection( | |
mouseSelection | |
) | |
textLabels = interactionDots.mark_text( | |
align='left', | |
fontSize=14, | |
dx = 7, | |
).encode( | |
alt.Text('Number of bed-places:Q', formatType='number'), | |
opacity = opacityCondition | |
) | |
countries_url = data.world_110m.url | |
countries = alt.topo_feature(countries_url, 'countries') | |
slider = alt.binding_range(min=1995, max=2021, step=1, name='Year: ') | |
year_selector = alt.selection_single( | |
name='year selector', | |
fields=['Year'], | |
bind=slider, | |
init={'Year': 2021} | |
) | |
worldMap = alt.Chart(countries).mark_geoshape( | |
fill = '#F2F3F4', | |
stroke = 'white', | |
strokeWidth = 0.5 | |
).properties( | |
width = 900, | |
height = 500, | |
).project( | |
'naturalEarth1' | |
) | |
circles = alt.Chart(df_bed_places_coords).mark_circle(size=100).encode( | |
latitude='latitude:Q', | |
longitude='longitude:Q', | |
tooltip=['Country:N','Year:O','Number of bed-places:Q'], | |
color='Number of bed-places:Q', | |
opacity=alt.condition(click_selector, alt.value(1), alt.value(0.4)), | |
size=alt.condition(click_selector, alt.value(200), alt.value(100)) | |
).transform_filter( | |
year_selector | |
).add_selection( | |
click_selector, | |
year_selector | |
) | |
circlesNoData = alt.Chart(df_bed_places_coords).mark_circle(size=100).encode( | |
latitude='latitude:Q', | |
longitude='longitude:Q', | |
tooltip=['Country:N','Year:O','Number of bed-places:Q'], | |
color= alt.value('lightgray'), | |
opacity=alt.condition(click_selector, alt.value(1), alt.value(0.4)), | |
size=alt.condition(click_selector, alt.value(200), alt.value(150)) | |
).transform_filter( | |
year_selector | |
).transform_filter( | |
alt.datum.dataAvailable == False | |
) | |
st.altair_chart((worldMap + circles + circlesNoData) & (bedPlaceChart + interactionDots + verticalLine + textLabels), use_container_width=True) | |
data = pd.read_csv('./data/hotel_booking_2019_2020.csv') | |
# %% | |
data_transform = data[['reservation_status_date','reservation_status']] | |
data_transform['reservation_status'] = (data['reservation_status'] == 'Canceled').astype(int) | |
data_transform['reservation_status_date'] = pd.to_datetime(data_transform['reservation_status_date']) | |
# %% | |
data_final = data_transform.groupby('reservation_status_date').count().reset_index() | |
# %% | |
line_chart = alt.Chart(data_final).mark_line().encode( | |
x=alt.X('reservation_status_date:T',title='Date'), | |
y=alt.Y('reservation_status:Q',title='Bookings'), | |
) | |
st.markdown(""" | |
A look into the amount of hotel booking across time to learn about the trends of hotel business trends. | |
""") | |
line_chart |