Spaces:
Sleeping
Sleeping
File size: 2,214 Bytes
def1e40 | 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 |
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Apply the default theme and activate color codes
sns.set_theme()
sns.set(color_codes=True)
################### import dataset
tips = sns.load_dataset("tips")
tips["tip_percentage"] = tips["tip"] / tips["total_bill"] * 100
########### set the title and subtitle
st.title("How does the amount of tip / percentage of tips differ across different days of the week?")
st.subheader("This app shows which days of the week bring in higher tip percentages and tip amounts, helping restaurants and staff adapt to customer tipping behavior and optimize their business.")
############## create filters for our interactive plot
with st.sidebar:
st.subheader("Filters")
# Day selection
all_days = sorted(tips["day"].unique())
selected_days = st.multiselect(
"Days to show",
options=all_days,
default=all_days,
)
# Feature options
feature_options = {
"Tip": "tip",
"Tip Percentage": "tip_percentage"
}
feature_label = st.selectbox("Feature (x-axis)", list(feature_options.keys()))
x_col = feature_options[feature_label]
# KDE options
fill = st.checkbox("Shade area", value=True)
if not selected_days:
st.info("Select at least one day to display the plot.")
else:
# Filter the data
data = tips[tips["day"].isin(selected_days)].dropna(subset=[x_col])
# KPI (numeric summary)
avg_value = data[x_col].mean()
unit = "$" if x_col == "tip" else "%"
st.metric(
label=f"Average {feature_label} for selected days",
value=f"{avg_value:.2f} {unit}"
)
# KDE plot
g = sns.displot(
data=data,
x=x_col,
hue="day",
kind="kde",
fill=fill
)
fig = g.fig
st.pyplot(fig)
plt.close(fig)
# Dynamic insight text
max_day = (
data.groupby("day")[x_col].mean()
.sort_values(ascending=False)
.index[0]
)
st.success(
f"💡 On average, **{max_day}** has the highest {feature_label.lower()} among the selected days."
)
|