Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,18 @@ st.write(df.head()) # Inspect the first few rows to see the column names and an
|
|
| 14 |
st.write("Missing values per column:")
|
| 15 |
st.write(df.isnull().sum()) # This will show the number of missing values per column
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Debug: Check the data types of the columns to see if any columns need to be converted
|
| 18 |
st.write("Data types of columns:")
|
| 19 |
st.write(df.dtypes)
|
|
@@ -47,21 +59,30 @@ df['Last Modified Date Year'] = pd.to_numeric(df['Last Modified Date Year'], err
|
|
| 47 |
# Convert object columns to category or proper types
|
| 48 |
for col in df.select_dtypes(include='object').columns:
|
| 49 |
df[col] = df[col].astype('category') # Convert strings to categories to improve performance
|
|
|
|
| 50 |
|
| 51 |
# Check for missing values after conversion
|
| 52 |
st.write("Missing values after conversion:")
|
| 53 |
st.write(df.isnull().sum()) # This will show the number of missing values per column
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Debug: Check the cleaned data after conversion
|
| 56 |
st.write("Cleaned data preview:")
|
| 57 |
st.write(df.head())
|
| 58 |
|
| 59 |
-
# Visualization 1: Bar chart of licenses by
|
| 60 |
-
st.subheader("Licenses by
|
| 61 |
|
| 62 |
-
# Group data by License
|
| 63 |
-
category_counts = df['License
|
| 64 |
-
category_counts.columns = ['License
|
| 65 |
|
| 66 |
# Debug: Check the grouped data for the first chart
|
| 67 |
st.write("Grouped data for licenses by type:")
|
|
@@ -70,22 +91,22 @@ st.write(category_counts.head()) # Check if data is available for plotting
|
|
| 70 |
# Ensure category_counts is not empty before plotting
|
| 71 |
if not category_counts.empty:
|
| 72 |
chart1 = alt.Chart(category_counts).mark_bar().encode(
|
| 73 |
-
x=alt.X('License
|
| 74 |
y=alt.Y('Count', title='Number of Licenses'),
|
| 75 |
-
color=alt.Color('License
|
| 76 |
).properties(
|
| 77 |
width=600,
|
| 78 |
height=400,
|
| 79 |
-
title="Number of Licenses by
|
| 80 |
)
|
| 81 |
st.altair_chart(chart1)
|
| 82 |
else:
|
| 83 |
-
st.write("No data available for the 'License
|
| 84 |
|
| 85 |
# Write-up for Visualization 1
|
| 86 |
st.write("""
|
| 87 |
-
**Licenses by
|
| 88 |
-
The bar chart was chosen for its simplicity in comparing counts across license
|
| 89 |
Colors were selected to make each bar distinct, and axis labels were added for easy interpretation.
|
| 90 |
If I had more time, I might add interactivity to allow users to filter by other variables.
|
| 91 |
""")
|
|
|
|
| 14 |
st.write("Missing values per column:")
|
| 15 |
st.write(df.isnull().sum()) # This will show the number of missing values per column
|
| 16 |
|
| 17 |
+
# Checking for percentage of missing values
|
| 18 |
+
missing_percentage = (df.isnull().sum() / len(df)) * 100
|
| 19 |
+
st.write("Percentage of Missing Values per Column", missing_percentage)
|
| 20 |
+
|
| 21 |
+
# Dropping columns with more than 90% missing values
|
| 22 |
+
df.drop(columns=['Title', 'Prefix', 'Suffix', 'BusinessDBA'], inplace=True)
|
| 23 |
+
|
| 24 |
+
# Dropping rows with missing values for important columns (where missing data is below 10%)
|
| 25 |
+
df.dropna(subset=['_id','License Number', 'First Name', 'Middle' , 'Last Name', 'Effective Date',
|
| 26 |
+
'Expiration Date', 'City', 'Zip', 'County', 'Specialty/Qualifier',
|
| 27 |
+
'Controlled Substance Schedule', 'Action'], inplace=True)
|
| 28 |
+
|
| 29 |
# Debug: Check the data types of the columns to see if any columns need to be converted
|
| 30 |
st.write("Data types of columns:")
|
| 31 |
st.write(df.dtypes)
|
|
|
|
| 59 |
# Convert object columns to category or proper types
|
| 60 |
for col in df.select_dtypes(include='object').columns:
|
| 61 |
df[col] = df[col].astype('category') # Convert strings to categories to improve performance
|
| 62 |
+
st.write(df.dtypes)
|
| 63 |
|
| 64 |
# Check for missing values after conversion
|
| 65 |
st.write("Missing values after conversion:")
|
| 66 |
st.write(df.isnull().sum()) # This will show the number of missing values per column
|
| 67 |
|
| 68 |
+
|
| 69 |
+
# Drop rows with any missing values
|
| 70 |
+
df.dropna(inplace=True)
|
| 71 |
+
|
| 72 |
+
# Verify if any missing values remain
|
| 73 |
+
st.write("Missing values after dropping rows with any missing values:")
|
| 74 |
+
st.write(df.isnull().sum()) # Verify if any missing values remain
|
| 75 |
+
|
| 76 |
# Debug: Check the cleaned data after conversion
|
| 77 |
st.write("Cleaned data preview:")
|
| 78 |
st.write(df.head())
|
| 79 |
|
| 80 |
+
# Visualization 1: Bar chart of licenses by Status
|
| 81 |
+
st.subheader("Licenses by Status")
|
| 82 |
|
| 83 |
+
# Group data by License Status
|
| 84 |
+
category_counts = df['License Status'].value_counts().reset_index()
|
| 85 |
+
category_counts.columns = ['License Status', 'Count']
|
| 86 |
|
| 87 |
# Debug: Check the grouped data for the first chart
|
| 88 |
st.write("Grouped data for licenses by type:")
|
|
|
|
| 91 |
# Ensure category_counts is not empty before plotting
|
| 92 |
if not category_counts.empty:
|
| 93 |
chart1 = alt.Chart(category_counts).mark_bar().encode(
|
| 94 |
+
x=alt.X('License Status', sort='-y', title='License Type'),
|
| 95 |
y=alt.Y('Count', title='Number of Licenses'),
|
| 96 |
+
color=alt.Color('License Status', legend=None)
|
| 97 |
).properties(
|
| 98 |
width=600,
|
| 99 |
height=400,
|
| 100 |
+
title="Number of Licenses by Status"
|
| 101 |
)
|
| 102 |
st.altair_chart(chart1)
|
| 103 |
else:
|
| 104 |
+
st.write("No data available for the 'License Status' plot.")
|
| 105 |
|
| 106 |
# Write-up for Visualization 1
|
| 107 |
st.write("""
|
| 108 |
+
**Licenses by Status**: This visualization highlights the distribution of licenses across status of the license.
|
| 109 |
+
The bar chart was chosen for its simplicity in comparing counts across license status. I chose to sort types in descending order for clarity.
|
| 110 |
Colors were selected to make each bar distinct, and axis labels were added for easy interpretation.
|
| 111 |
If I had more time, I might add interactivity to allow users to filter by other variables.
|
| 112 |
""")
|