-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode 3.py
More file actions
65 lines (52 loc) · 3.09 KB
/
Copy pathcode 3.py
File metadata and controls
65 lines (52 loc) · 3.09 KB
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
import pandas as pd
import plotly.express as px
# --- Part 1: Your Manually Provided Daily Data ---
data = {
'Day': ['Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
'Date': ['01-May-25', '02-May-25', '03-May-25', '04-May-25', '05-May-25', '06-May-25', '07-May-25', '08-May-25', '09-May-25', '10-May-25', '11-May-25', '12-May-25', '13-May-25', '14-May-25', '15-May-25', '16-May-25', '17-May-25', '18-May-25', '19-May-25', '20-May-25', '21-May-25', '22-May-25', '23-May-25', '24-May-25', '25-May-25', '26-May-25', '27-May-25', '28-May-25', '29-May-25', '30-May-25', '31-May-25'],
'Total Appointments': ['14,48,809', '13,56,308', '43,691', '4,170', '4,390', '16,37,275', '14,92,227', '14,42,099', '13,65,010', '60,038', '3,564', '16,09,558', '15,08,795', '14,13,453', '13,89,367', '13,54,207', '53,938', '4,282', '15,90,561', '15,22,001', '14,13,278', '14,05,391', '13,35,196', '43,708', '3,726', '3,957', '15,26,167', '13,94,945', '13,64,783', '12,69,771', '49,399'],
'Did Not Attend': ['63,012', '55,884', '3,577', '213', '213', '69,943', '60,715', '58,770', '54,078', '4,503', '157', '61,558', '62,567', '57,575', '57,506', '53,560', '4,516', '284', '59,226', '62,461', '57,971', '57,728', '53,749', '3,850', '102', '177', '68,262', '59,160', '57,588', '52,050', '3,942']
}
df = pd.DataFrame(data)
# --- Part 2: Clean Data and Calculate DNA Rate ---
# Remove commas and convert to numbers
for col in ['Total Appointments', 'Did Not Attend']:
df[col] = df[col].str.replace(',', '').astype(int)
# Calculate the DNA rate for each day
df['DNA_Rate'] = df['Did Not Attend'] / df['Total Appointments']
# --- Part 3: Analyze by Day of the Week ---
# Group by the day of the week and calculate the average DNA rate
daily_avg_dna = df.groupby('Day')['DNA_Rate'].mean().reset_index()
# Sort the days for a logical chart order
day_order = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
daily_avg_dna['Day'] = pd.Categorical(daily_avg_dna['Day'], categories=day_order, ordered=True)
daily_avg_dna = daily_avg_dna.sort_values('Day')
print("✅ Average DNA Rate by Day of the Week calculated.")
print(daily_avg_dna)
# --- Part 4: Create the Visualization ---
chart_title = "Average 'Did Not Attend' Rate by Day of the Week (May 2025)"
fig = px.bar(
daily_avg_dna,
x='Day',
y='DNA_Rate',
title=chart_title,
labels={'DNA_Rate': "Average 'Did Not Attend' Rate", 'Day': 'Day of the Week'},
text='DNA_Rate'
)
# Format the graph for clarity
fig.update_layout(
title_font_size=22,
plot_bgcolor='white',
yaxis_tickformat='.1%' # Format y-axis as percentage
)
fig.update_traces(
texttemplate='%{text:.1%}',
textposition='outside',
marker_color='#5cb85c'
)
# --- Part 5: Save the Plot ---
try:
fig.write_image("dna_by_day_of_week.png", width=1000, height=600, scale=2)
print("\n✅ Chart saved to 'dna_by_day_of_week.png'")
except ValueError as e:
print(f"\n❌ Error saving image: {e}. Please ensure 'kaleido' is installed.")