File size: 2,096 Bytes
9b17fda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

#Import
import streamlit as st
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

#Clean data
tips['tip_percentage'] = tips['tip'] / tips['total_bill'] * 100
tips_cleaned = tips.drop_duplicates(keep='first')

#Titles
st.title('What characteristics make a good tipper?')

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")

#Sidebar
with st.sidebar:
    st.subheader('filters')
    both_genders = sorted(tips_cleaned['sex'].unique().tolist())
    smoker_all = sorted(tips_cleaned['smoker'].unique().tolist())

    selected_gender = st.multiselect('Genders to show', options=both_genders,default=both_genders)
    selected_smoker = st.multiselect('Smoker status', options=smoker_all, default=smoker_all)


    feature_options = {
        "tip percentage": "tip_percentage",
        "tip amount": "tip"
    }
    feature_label = st.selectbox("Feature (y-axis)", list(feature_options.keys()))
    y_col = feature_options[feature_label]

#Filtered data
data = tips_cleaned[
    (tips_cleaned["sex"].isin(selected_gender)) &
    (tips_cleaned["smoker"].isin(selected_smoker)) 
].dropna(subset=[y_col])

#KPI
mean_val = data[y_col].mean()

st.metric(
    label=f'Average {feature_label}',
    value=f'{mean_val:.2f}'
)

#Plot
p = sns.relplot(data=data, x='total_bill', y=y_col, hue='sex')
p.figure.suptitle("Relationship between total bill and the amount tipped")
st.pyplot(p.figure)

#More stats
median_val = data[y_col].median()
count_val = len(data)

#Dynamic text
if y_col == "tip_percentage":
    st.markdown(
        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."
    )
else:
    st.markdown(
        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."
    )