Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app_tips.py +70 -0
- requirements.txt +4 -0
app_tips.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
#Import
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
tips = sns.load_dataset("tips")
|
| 10 |
+
|
| 11 |
+
#Clean data
|
| 12 |
+
tips['tip_percentage'] = tips['tip'] / tips['total_bill'] * 100
|
| 13 |
+
tips_cleaned = tips.drop_duplicates(keep='first')
|
| 14 |
+
|
| 15 |
+
#Titles
|
| 16 |
+
st.title('What characteristics make a good tipper?')
|
| 17 |
+
|
| 18 |
+
st.subheader("The aim of this page is to enable users to explore whether any of the given characteristics make a good tipper based on the available data")
|
| 19 |
+
|
| 20 |
+
#Sidebar
|
| 21 |
+
with st.sidebar:
|
| 22 |
+
st.subheader('filters')
|
| 23 |
+
both_genders = sorted(tips_cleaned['sex'].unique().tolist())
|
| 24 |
+
smoker_all = sorted(tips_cleaned['smoker'].unique().tolist())
|
| 25 |
+
|
| 26 |
+
selected_gender = st.multiselect('Genders to show', options=both_genders,default=both_genders)
|
| 27 |
+
selected_smoker = st.multiselect('Smoker status', options=smoker_all, default=smoker_all)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
feature_options = {
|
| 31 |
+
"tip percentage": "tip_percentage",
|
| 32 |
+
"tip amount": "tip"
|
| 33 |
+
}
|
| 34 |
+
feature_label = st.selectbox("Feature (y-axis)", list(feature_options.keys()))
|
| 35 |
+
y_col = feature_options[feature_label]
|
| 36 |
+
|
| 37 |
+
#Filtered data
|
| 38 |
+
data = tips_cleaned[
|
| 39 |
+
(tips_cleaned["sex"].isin(selected_gender)) &
|
| 40 |
+
(tips_cleaned["smoker"].isin(selected_smoker))
|
| 41 |
+
].dropna(subset=[y_col])
|
| 42 |
+
|
| 43 |
+
#KPI
|
| 44 |
+
mean_val = data[y_col].mean()
|
| 45 |
+
|
| 46 |
+
st.metric(
|
| 47 |
+
label=f'Average {feature_label}',
|
| 48 |
+
value=f'{mean_val:.2f}'
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
#Plot
|
| 52 |
+
p = sns.relplot(data=data, x='total_bill', y=y_col, hue='sex')
|
| 53 |
+
p.figure.suptitle("Relationship between total bill and the amount tipped")
|
| 54 |
+
st.pyplot(p.figure)
|
| 55 |
+
|
| 56 |
+
#More stats
|
| 57 |
+
median_val = data[y_col].median()
|
| 58 |
+
count_val = len(data)
|
| 59 |
+
|
| 60 |
+
#Dynamic text
|
| 61 |
+
if y_col == "tip_percentage":
|
| 62 |
+
st.markdown(
|
| 63 |
+
f"Based on the current filters, the average tip percentage is {mean_val:.2f}%, with a median of {median_val:.2f}% across {int(count_val)} bills."
|
| 64 |
+
)
|
| 65 |
+
else:
|
| 66 |
+
st.markdown(
|
| 67 |
+
f"Based on the current filters, the average tip amount is {mean_val:.2f} dollars, with a median of {median_val:.2f} dollars across {int(count_val)} bills."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas == 2.3.2
|
| 2 |
+
seaborn == 0.13.2
|
| 3 |
+
matplotlib == 3.9.2
|
| 4 |
+
streamlit == 1.49.1
|