Spaces:
Runtime error
Runtime error
File size: 6,159 Bytes
74addf2 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
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 |