MacMahesh commited on
Commit
9749cc5
·
verified ·
1 Parent(s): 2dc08cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -34
app.py CHANGED
@@ -1,43 +1,59 @@
1
- import streamlit as st
2
- import seaborn as sns
3
  import pandas as pd
 
 
 
 
 
 
 
4
 
5
- # Load data
6
- data = sns.load_dataset("tips")
 
7
 
8
- # Page config
9
- st.set_page_config(page_title='Tips Data Analysis', layout='wide')
 
10
 
11
- # Title and description
12
- st.title('Tips Data Analysis with Streamlit')
13
- st.write('Tips rise with total bill, but the tip/total_bill ratio goes down as total bill increases.')
14
- st.write('---')
15
 
16
- # Sidebar widgets
17
  with st.sidebar:
18
- st.subheader('Filter Options')
19
- threshold = st.slider('Threshold for Total Bill', min_value=0, max_value=100, value=10, step=1)
20
- day_selector = st.multiselect('Select Day(s)', options=data['day'].unique(), default=data['day'].unique())
21
-
22
- # Filter data
23
- filtered_data = data[data['total_bill'] > threshold]
24
- filtered_data = filtered_data[filtered_data['day'].isin(day_selector)]
25
-
26
- # Main content
27
- st.subheader('Total Bill vs Tip Scatter Plot')
28
-
29
- if not filtered_data.empty:
30
- current_selected_day = ', '.join(filtered_data['day'].unique())
31
- st.write(f"Scatter plot for total bill vs tip for {current_selected_day} with total bill above {threshold}.")
32
- st.write(
33
- f"You can see current average tip {filtered_data['tip'].mean():.2f} for selected days. "
34
- f"But tip/total_bill ratio is {(filtered_data['tip']/filtered_data['total_bill']).mean():.2%}"
35
- )
36
- st.scatter_chart(data=filtered_data, x='total_bill', y='tip', color='day')
37
- st.dataframe(filtered_data)
 
 
 
 
 
 
 
 
 
 
 
 
38
  else:
39
- st.warning("No data available for the selected filters.")
40
-
41
- st.write('---')
42
 
43
 
 
 
 
1
  import pandas as pd
2
+ import seaborn as sns
3
+ import matplotlib.pyplot as plt
4
+ import streamlit as st
5
+
6
+ # -------------------------
7
+ # Streamlit App — Tipping
8
+ # -------------------------
9
 
10
+ # Load dataset
11
+ tips = sns.load_dataset("tips")
12
+ tips["tip_pct"] = tips["tip"] / tips["total_bill"] * 100
13
 
14
+ # User question
15
+ st.title("💡 Do people tip more on certain days of the week?")
16
+ st.subheader("Explore average tip percentages by day, time.")
17
 
18
+ # Short problem statement
19
+ st.write("This app helps answer whether tipping behavior changes depending on the day of the week. "
20
+ "Use the filters to explore differences by day and time(Lunch/Dinner) of day.")
 
21
 
22
+ # Sidebar filters
23
  with st.sidebar:
24
+ st.subheader("Filters")
25
+ # Day filter
26
+ all_days = sorted(tips["day"].dropna().unique().tolist())
27
+ selected_days = st.multiselect("Days to show", options=all_days, default=all_days)
28
+
29
+ # Time filter
30
+ all_times = tips["time"].dropna().unique().tolist()
31
+ selected_time = st.selectbox("Select time of day", options=["All"] + all_times)
32
+
33
+
34
+ # Apply filters
35
+ filtered = tips[tips["day"].isin(selected_days)]
36
+ if selected_time != "All":
37
+ filtered = filtered[filtered["time"] == selected_time]
38
+
39
+
40
+ # KPI: average tip percentage
41
+ avg_tip = filtered["tip_pct"].mean()
42
+ st.metric("For selected days and time , Average Tip %", f"{avg_tip:.2f}%")
43
+
44
+ # Visualization
45
+ if not filtered.empty:
46
+ plt.figure(figsize=(6,4))
47
+ sns.boxplot(x="day", y="tip_pct", data=filtered, order=all_days)
48
+ plt.title("Tip Percentage by Day")
49
+ st.pyplot(plt.gcf())
50
+ plt.close()
51
+
52
+ # Dynamic insight
53
+ best_day = filtered.groupby("day")["tip_pct"].mean().idxmax()
54
+ best_value = filtered.groupby("day")["tip_pct"].mean().max()
55
+ st.success(f"💡 Insight: On average, for {selected_time}, {best_day} has the highest tip percentage at {best_value:.2f}%")
56
  else:
57
+ st.info("No data available for the selected filters.")
 
 
58
 
59