Spaces:
Sleeping
Sleeping
Delete app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178# import streamlit as st# import pandas as pd# import altair as alt
# # Loading the Dataset# url = 'https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/licenses_fall2022.csv'# df = pd.read_csv(url)
# # Convert object columns to string for compatibility# for col in df.select_dtypes(include=['object']).columns:# df[col] = df[col].astype(str)
# # Convert integer columns to float# for col in df.select_dtypes(include=['int64', 'float64']).columns:# df[col] = pd.to_numeric(df[col], errors='coerce')
# # Checking for missing values and handling them# df['Middle'] = df['Middle'].fillna('N/A') # Update Middle column without inplace
# # Dropping columns with more than 90% missing values# df.drop(columns=['Title', 'Prefix', 'Suffix', 'BusinessDBA'], inplace=True)
# # Dropping rows with missing values for certain key columns# df.dropna(subset=['License Number', 'First Name', 'Last Name', 'Effective Date',# 'Expiration Date', 'City', 'Zip', 'County', 'Specialty/Qualifier',# 'Controlled Substance Schedule', 'Action'], inplace=True)
# # Convert datetime columns# date_columns = ['Original Issue Date', 'Effective Date', 'Expiration Date', 'LastModifiedDate']# for col in date_columns:# df[col] = pd.to_datetime(df[col], errors='coerce', format='%m/%d/%Y')
# # Add year columns# df['Original Issue Year'] = df['Original Issue Date'].dt.year# df['Effective Date Year'] = df['Effective Date'].dt.year# df['Expiration Date Year'] = df['Expiration Date'].dt.year# df['Last Modified Date Year'] = df['LastModifiedDate'].dt.year
# # Final check of data types# st.write("Column Data Types:", df.dtypes)
# # # Converting object columns to category for better memory management# # for col in df.select_dtypes(include='object').columns:# # df[col] = df[col].astype('category')
# # Visualization 1: Licenses by Type with License Status breakdown# st.subheader("Licenses by Type and Status")
# # Group data by License Type and License Status# category_status_counts = df.groupby(['License Type', 'License Status']).size().reset_index(name='Count')
# # Create a bar chart with color representing License Status# chart1 = alt.Chart(category_status_counts).mark_bar().encode(# x=alt.X('License Type', sort='-y', title='License Type'),# y=alt.Y('Count', title='Number of Licenses'),# color=alt.Color('License Status', legend=alt.Legend(title="License Status")),# tooltip=['License Type', 'License Status', 'Count']# ).properties(# width=700,# height=400,# title="Number of Licenses by Type and Status"# ).interactive()
# st.altair_chart(chart1)
# # Write-up for Visualization 1# st.write("""# **Licenses by Type and Status**: This visualization shows the distribution of licenses across different types with further breakdown by license status.# The interactive chart allows users to view counts for each license status (e.g., active, inactive) within each license type.# 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.# """)
# # Visualization 2: Line chart showing licenses issued over time# st.subheader("Licenses Over Time")
# # Ensure the '_id' column exists; otherwise, use another unique identifier if available# if '_id' in df.columns:# time_data = df.groupby('Original Issue Year')['_id'].count().reset_index()# else:# time_data = df.groupby('Original Issue Year').size().reset_index(name='License Count')
# time_data.columns = ['Year', 'License Count']
# chart2 = alt.Chart(time_data).mark_line().encode(# x=alt.X('Year', title='Year'),# y=alt.Y('License Count', title='Number of Licenses'),# color=alt.value('blue')# ).properties(# width=600,# height=400,# title="Number of Licenses Issued Over Time"# )# st.altair_chart(chart2)
# # Write-up for Visualization 2# st.write("""# **Licenses Over Time**: This line chart visualizes the trend in the number of licenses issued over time.# 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.# If I had more time, I would explore monthly or seasonal patterns by adjusting the time grouping.# """)
import streamlit as stimport pandas as pdimport altair as alt
# Loading the Dataseturl = 'https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/licenses_fall2022.csv'df = pd.read_csv(url)
# Dropping problematic columnsdf.drop(columns=['_id', 'Case Number', 'Delegated Controlled Substance Schedule'], inplace=True)
# Handling missing datadf.dropna(subset=['License Number', 'First Name', 'Middle' , 'Last Name', 'Effective Date', 'Expiration Date', 'City', 'Zip', 'County', 'Specialty/Qualifier', 'Controlled Substance Schedule', 'Action'], inplace=True)
# Converting date columns to datetime formatdf['Original Issue Date'] = pd.to_datetime(df['Original Issue Date'], errors='coerce', format='%m/%d/%Y')df['Effective Date'] = pd.to_datetime(df['Effective Date'], errors='coerce', format='%m/%d/%Y')df['Expiration Date'] = pd.to_datetime(df['Expiration Date'], errors='coerce', format='%m/%d/%Y')df['LastModifiedDate'] = pd.to_datetime(df['LastModifiedDate'], errors='coerce', format='%m/%d/%Y')
# Adding Year columnsdf['Original Issue Year'] = df['Original Issue Date'].dt.yeardf['Effective Date Year'] = df['Effective Date'].dt.yeardf['Expiration Date Year'] = df['Expiration Date'].dt.yeardf['Last Modified Date Year'] = df['LastModifiedDate'].dt.year
# Converting object columns to categoryfor col in df.select_dtypes(include='object').columns: df[col] = df[col].astype('category')
import plotly.express as px
# Visualization 1: Treemap of licenses by typest.subheader("License Type Distribution (Treemap)")
# Creating the treemapfig = px.treemap(category_counts, path=['License Type'], values='Count', color='License Type', title="License Type Distribution Treemap", color_discrete_sequence=px.colors.qualitative.Set3)
# Display the treemapst.plotly_chart(fig)
# Write-up for Visualization 1st.write("""**License Type Distribution (Treemap)**: This treemap visualization helps us understand the distribution of license types.Each rectangle's size represents the count of each license type, and the color indicates different categories. This method is especially helpful when you want to explore proportions in a more engaging and interactive manner.""")
# Visualization 2: Bar chart of licenses issued over timest.subheader("Licenses Issued Over Time (Bar Chart)")
# Grouping data by year for a bar charttime_data = df.groupby('Original Issue Year')['_id'].count().reset_index()time_data.columns = ['Year', 'License Count']
# Creating the bar chartchart2 = alt.Chart(time_data).mark_bar().encode( x=alt.X('Year', title='Year'), y=alt.Y('License Count', title='Number of Licenses'), color=alt.Color('Year', legend=None),).properties( width=600, height=400, title="Licenses Issued Over Time (Bar Chart)")
# Displaying the chartst.altair_chart(chart2)
# Write-up for Visualization 2st.write("""**Licenses Issued Over Time**: This bar chart illustrates the number of licenses issued each year.The bar chart emphasizes year-by-year changes and allows for easy comparison. By coloring the bars based on the year, the chart visually distinguishes different years, making it more impactful.""")app.pyDeleted-178