ralate2 commited on
Commit
26cd9b0
·
verified ·
1 Parent(s): ca6aa96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -63
app.py CHANGED
@@ -1,3 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import altair as alt
@@ -6,95 +108,73 @@ import altair as alt
6
  url = 'https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/licenses_fall2022.csv'
7
  df = pd.read_csv(url)
8
 
9
- # Convert object columns to string for compatibility
10
- for col in df.select_dtypes(include=['object']).columns:
11
- df[col] = df[col].astype(str)
12
-
13
- # Convert integer columns to float
14
- for col in df.select_dtypes(include=['int64', 'float64']).columns:
15
- df[col] = pd.to_numeric(df[col], errors='coerce')
16
-
17
- # Checking for missing values and handling them
18
- df['Middle'] = df['Middle'].fillna('N/A') # Update Middle column without inplace
19
 
20
- # Dropping columns with more than 90% missing values
21
- df.drop(columns=['Title', 'Prefix', 'Suffix', 'BusinessDBA'], inplace=True)
 
22
 
23
- # Dropping rows with missing values for certain key columns
24
- df.dropna(subset=['License Number', 'First Name', 'Last Name', 'Effective Date',
25
- 'Expiration Date', 'City', 'Zip', 'County', 'Specialty/Qualifier',
26
- 'Controlled Substance Schedule', 'Action'], inplace=True)
 
27
 
28
- # Convert datetime columns
29
- date_columns = ['Original Issue Date', 'Effective Date', 'Expiration Date', 'LastModifiedDate']
30
- for col in date_columns:
31
- df[col] = pd.to_datetime(df[col], errors='coerce', format='%m/%d/%Y')
32
-
33
- # Add year columns
34
  df['Original Issue Year'] = df['Original Issue Date'].dt.year
35
  df['Effective Date Year'] = df['Effective Date'].dt.year
36
  df['Expiration Date Year'] = df['Expiration Date'].dt.year
37
  df['Last Modified Date Year'] = df['LastModifiedDate'].dt.year
38
 
39
- # Final check of data types
40
- st.write("Column Data Types:", df.dtypes)
41
-
42
- # # Converting object columns to category for better memory management
43
- # for col in df.select_dtypes(include='object').columns:
44
- # df[col] = df[col].astype('category')
45
 
46
- # Visualization 1: Licenses by Type with License Status breakdown
47
- st.subheader("Licenses by Type and Status")
48
 
49
- # Group data by License Type and License Status
50
- category_status_counts = df.groupby(['License Type', 'License Status']).size().reset_index(name='Count')
 
51
 
52
- # Create a bar chart with color representing License Status
53
- chart1 = alt.Chart(category_status_counts).mark_bar().encode(
54
  x=alt.X('License Type', sort='-y', title='License Type'),
55
- y=alt.Y('Count', title='Number of Licenses'),
56
- color=alt.Color('License Status', legend=alt.Legend(title="License Status")),
57
- tooltip=['License Type', 'License Status', 'Count']
58
  ).properties(
59
  width=700,
60
  height=400,
61
- title="Number of Licenses by Type and Status"
62
- ).interactive()
63
-
64
  st.altair_chart(chart1)
65
 
66
- # Write-up for Visualization 1
67
  st.write("""
68
- **Licenses by Type and Status**: This visualization shows the distribution of licenses across different types with further breakdown by license status.
69
- The interactive chart allows users to view counts for each license status (e.g., active, inactive) within each license type.
70
- This breakdown can help identify how license types vary not only in volume but also in the proportion of active versus inactive licenses.If I had more time, I might add interactivity to allow users to filter by other variables.
71
  """)
72
 
73
- # Visualization 2: Line chart showing licenses issued over time
74
- st.subheader("Licenses Over Time")
75
-
76
- # Ensure the '_id' column exists; otherwise, use another unique identifier if available
77
- if '_id' in df.columns:
78
- time_data = df.groupby('Original Issue Year')['_id'].count().reset_index()
79
- else:
80
- time_data = df.groupby('Original Issue Year').size().reset_index(name='License Count')
81
 
82
- time_data.columns = ['Year', 'License Count']
 
83
 
84
- chart2 = alt.Chart(time_data).mark_line().encode(
85
- x=alt.X('Year', title='Year'),
86
- y=alt.Y('License Count', title='Number of Licenses'),
87
- color=alt.value('blue')
 
88
  ).properties(
89
- width=600,
90
  height=400,
91
  title="Number of Licenses Issued Over Time"
92
- )
93
  st.altair_chart(chart2)
94
 
95
- # Write-up for Visualization 2
96
  st.write("""
97
- **Licenses Over Time**: This line chart visualizes the trend in the number of licenses issued over time.
98
- I chose a line chart to emphasize changes over years. Blue was used to create a consistent look. Axis labels and title are provided for clarity.
99
- If I had more time, I would explore monthly or seasonal patterns by adjusting the time grouping.
100
  """)
 
1
+ # import streamlit as st
2
+ # import pandas as pd
3
+ # import altair as alt
4
+
5
+ # # Loading the Dataset
6
+ # url = 'https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/licenses_fall2022.csv'
7
+ # df = pd.read_csv(url)
8
+
9
+ # # Convert object columns to string for compatibility
10
+ # for col in df.select_dtypes(include=['object']).columns:
11
+ # df[col] = df[col].astype(str)
12
+
13
+ # # Convert integer columns to float
14
+ # for col in df.select_dtypes(include=['int64', 'float64']).columns:
15
+ # df[col] = pd.to_numeric(df[col], errors='coerce')
16
+
17
+ # # Checking for missing values and handling them
18
+ # df['Middle'] = df['Middle'].fillna('N/A') # Update Middle column without inplace
19
+
20
+ # # Dropping columns with more than 90% missing values
21
+ # df.drop(columns=['Title', 'Prefix', 'Suffix', 'BusinessDBA'], inplace=True)
22
+
23
+ # # Dropping rows with missing values for certain key columns
24
+ # df.dropna(subset=['License Number', 'First Name', 'Last Name', 'Effective Date',
25
+ # 'Expiration Date', 'City', 'Zip', 'County', 'Specialty/Qualifier',
26
+ # 'Controlled Substance Schedule', 'Action'], inplace=True)
27
+
28
+ # # Convert datetime columns
29
+ # date_columns = ['Original Issue Date', 'Effective Date', 'Expiration Date', 'LastModifiedDate']
30
+ # for col in date_columns:
31
+ # df[col] = pd.to_datetime(df[col], errors='coerce', format='%m/%d/%Y')
32
+
33
+ # # Add year columns
34
+ # df['Original Issue Year'] = df['Original Issue Date'].dt.year
35
+ # df['Effective Date Year'] = df['Effective Date'].dt.year
36
+ # df['Expiration Date Year'] = df['Expiration Date'].dt.year
37
+ # df['Last Modified Date Year'] = df['LastModifiedDate'].dt.year
38
+
39
+ # # Final check of data types
40
+ # st.write("Column Data Types:", df.dtypes)
41
+
42
+ # # # Converting object columns to category for better memory management
43
+ # # for col in df.select_dtypes(include='object').columns:
44
+ # # df[col] = df[col].astype('category')
45
+
46
+ # # Visualization 1: Licenses by Type with License Status breakdown
47
+ # st.subheader("Licenses by Type and Status")
48
+
49
+ # # Group data by License Type and License Status
50
+ # category_status_counts = df.groupby(['License Type', 'License Status']).size().reset_index(name='Count')
51
+
52
+ # # Create a bar chart with color representing License Status
53
+ # chart1 = alt.Chart(category_status_counts).mark_bar().encode(
54
+ # x=alt.X('License Type', sort='-y', title='License Type'),
55
+ # y=alt.Y('Count', title='Number of Licenses'),
56
+ # color=alt.Color('License Status', legend=alt.Legend(title="License Status")),
57
+ # tooltip=['License Type', 'License Status', 'Count']
58
+ # ).properties(
59
+ # width=700,
60
+ # height=400,
61
+ # title="Number of Licenses by Type and Status"
62
+ # ).interactive()
63
+
64
+ # st.altair_chart(chart1)
65
+
66
+ # # Write-up for Visualization 1
67
+ # st.write("""
68
+ # **Licenses by Type and Status**: This visualization shows the distribution of licenses across different types with further breakdown by license status.
69
+ # The interactive chart allows users to view counts for each license status (e.g., active, inactive) within each license type.
70
+ # This breakdown can help identify how license types vary not only in volume but also in the proportion of active versus inactive licenses.If I had more time, I might add interactivity to allow users to filter by other variables.
71
+ # """)
72
+
73
+ # # Visualization 2: Line chart showing licenses issued over time
74
+ # st.subheader("Licenses Over Time")
75
+
76
+ # # Ensure the '_id' column exists; otherwise, use another unique identifier if available
77
+ # if '_id' in df.columns:
78
+ # time_data = df.groupby('Original Issue Year')['_id'].count().reset_index()
79
+ # else:
80
+ # time_data = df.groupby('Original Issue Year').size().reset_index(name='License Count')
81
+
82
+ # time_data.columns = ['Year', 'License Count']
83
+
84
+ # chart2 = alt.Chart(time_data).mark_line().encode(
85
+ # x=alt.X('Year', title='Year'),
86
+ # y=alt.Y('License Count', title='Number of Licenses'),
87
+ # color=alt.value('blue')
88
+ # ).properties(
89
+ # width=600,
90
+ # height=400,
91
+ # title="Number of Licenses Issued Over Time"
92
+ # )
93
+ # st.altair_chart(chart2)
94
+
95
+ # # Write-up for Visualization 2
96
+ # st.write("""
97
+ # **Licenses Over Time**: This line chart visualizes the trend in the number of licenses issued over time.
98
+ # I chose a line chart to emphasize changes over years. Blue was used to create a consistent look. Axis labels and title are provided for clarity.
99
+ # If I had more time, I would explore monthly or seasonal patterns by adjusting the time grouping.
100
+ # """)
101
+
102
+
103
  import streamlit as st
104
  import pandas as pd
105
  import altair as alt
 
108
  url = 'https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/licenses_fall2022.csv'
109
  df = pd.read_csv(url)
110
 
111
+ # Dropping problematic columns
112
+ df.drop(columns=['_id', 'Case Number', 'Delegated Controlled Substance Schedule'], inplace=True)
 
 
 
 
 
 
 
 
113
 
114
+ # Handling missing data
115
+ df['Middle'].fillna('N/A', inplace=True)
116
+ df.dropna(subset=['License Number', 'First Name', 'Last Name', 'Effective Date', 'Expiration Date', 'City', 'Zip', 'County', 'Specialty/Qualifier', 'Controlled Substance Schedule', 'Action'], inplace=True)
117
 
118
+ # Converting date columns to datetime format
119
+ df['Original Issue Date'] = pd.to_datetime(df['Original Issue Date'], errors='coerce', format='%m/%d/%Y')
120
+ df['Effective Date'] = pd.to_datetime(df['Effective Date'], errors='coerce', format='%m/%d/%Y')
121
+ df['Expiration Date'] = pd.to_datetime(df['Expiration Date'], errors='coerce', format='%m/%d/%Y')
122
+ df['LastModifiedDate'] = pd.to_datetime(df['LastModifiedDate'], errors='coerce', format='%m/%d/%Y')
123
 
124
+ # Adding Year columns
 
 
 
 
 
125
  df['Original Issue Year'] = df['Original Issue Date'].dt.year
126
  df['Effective Date Year'] = df['Effective Date'].dt.year
127
  df['Expiration Date Year'] = df['Expiration Date'].dt.year
128
  df['Last Modified Date Year'] = df['LastModifiedDate'].dt.year
129
 
130
+ # Converting object columns to category
131
+ for col in df.select_dtypes(include='object').columns:
132
+ df[col] = df[col].astype('category')
 
 
 
133
 
134
+ # Visualization 1: Distribution of License Types (Enhanced)
135
+ st.subheader("Distribution of License Types (Log Scale)")
136
 
137
+ # Aggregating and plotting data
138
+ license_type_counts = df['License Type'].value_counts().reset_index()
139
+ license_type_counts.columns = ['License Type', 'Count']
140
 
141
+ chart1 = alt.Chart(license_type_counts).mark_bar().encode(
 
142
  x=alt.X('License Type', sort='-y', title='License Type'),
143
+ y=alt.Y('Count', title='Number of Licenses (Log Scale)', scale=alt.Scale(type='log')),
144
+ color=alt.Color('License Type', legend=None)
 
145
  ).properties(
146
  width=700,
147
  height=400,
148
+ title="Distribution of License Types (Log Scale)"
149
+ )
 
150
  st.altair_chart(chart1)
151
 
152
+ # Writeup for Visualization 1
153
  st.write("""
154
+ **Distribution of License Types**: This bar chart (using a log scale for clarity) displays the distribution of different license types.
155
+ Due to the imbalance in the dataset, a logarithmic scale is applied to better represent both common and rare license types.
 
156
  """)
157
 
158
+ # Visualization 2: Trend of Licenses Issued Over Time (Enhanced)
159
+ st.subheader("Trend of Licenses Issued Over Time (Interactive)")
 
 
 
 
 
 
160
 
161
+ # Aggregating yearly data
162
+ time_data = df.groupby('Original Issue Year').size().reset_index(name='License Count')
163
 
164
+ # Plotting the trend with additional interactivity
165
+ chart2 = alt.Chart(time_data).mark_line(point=True).encode(
166
+ x=alt.X('Original Issue Year:O', title='Year'),
167
+ y=alt.Y('License Count', title='Number of Licenses Issued'),
168
+ tooltip=['Original Issue Year', 'License Count']
169
  ).properties(
170
+ width=700,
171
  height=400,
172
  title="Number of Licenses Issued Over Time"
173
+ ).interactive()
174
  st.altair_chart(chart2)
175
 
176
+ # Writeup for Visualization 2
177
  st.write("""
178
+ **Trend of Licenses Issued Over Time**: This line chart visualizes the trend in the number of licenses issued each year.
179
+ Interactive points allow viewers to see the specific count for each year. This chart provides insight into trends in licensing activity over time.
 
180
  """)