ralate2 commited on
Commit
843a777
·
verified ·
1 Parent(s): 0909a2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -6,11 +6,17 @@ 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
- # Dropping problematic columns
10
- df.drop(columns=['_id', 'Case Number', 'Delegated Controlled Substance Schedule'], inplace=True)
 
11
 
12
- # Handling missing data
13
- df.dropna(subset=['License Number', 'First Name', 'Middle', 'Last Name', 'Effective Date', 'Expiration Date', 'City', 'Zip', 'County', 'Specialty/Qualifier', 'Controlled Substance Schedule', 'Action'], inplace=True)
 
 
 
 
 
14
 
15
  # Converting date columns to datetime format
16
  df['Original Issue Date'] = pd.to_datetime(df['Original Issue Date'], errors='coerce', format='%m/%d/%Y')
@@ -18,19 +24,32 @@ df['Effective Date'] = pd.to_datetime(df['Effective Date'], errors='coerce', for
18
  df['Expiration Date'] = pd.to_datetime(df['Expiration Date'], errors='coerce', format='%m/%d/%Y')
19
  df['LastModifiedDate'] = pd.to_datetime(df['LastModifiedDate'], errors='coerce', format='%m/%d/%Y')
20
 
21
- # Adding Year columns
 
 
 
 
 
 
 
 
22
  df['Original Issue Year'] = df['Original Issue Date'].dt.year
23
  df['Effective Date Year'] = df['Effective Date'].dt.year
24
  df['Expiration Date Year'] = df['Expiration Date'].dt.year
25
  df['Last Modified Date Year'] = df['LastModifiedDate'].dt.year
26
 
27
- # Converting object columns to category
 
 
 
 
28
  for col in df.select_dtypes(include='object').columns:
29
- df[col] = df[col].astype('category')
 
30
 
31
- # Debug: Check the data after preprocessing
32
- st.write("Data after preprocessing:")
33
- st.write(df.head()) # Check the first few rows
34
 
35
  # Visualization 1: Bar chart of licenses by type
36
  st.subheader("Licenses by Type")
 
6
  url = 'https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/licenses_fall2022.csv'
7
  df = pd.read_csv(url)
8
 
9
+ # Debug: Check the first few rows of the original dataset to understand its structure
10
+ st.write("Original data preview:")
11
+ st.write(df.head()) # Inspect the first few rows to see the column names and any potential issues
12
 
13
+ # Check for missing values in each column
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)
20
 
21
  # Converting date columns to datetime format
22
  df['Original Issue Date'] = pd.to_datetime(df['Original Issue Date'], errors='coerce', format='%m/%d/%Y')
 
24
  df['Expiration Date'] = pd.to_datetime(df['Expiration Date'], errors='coerce', format='%m/%d/%Y')
25
  df['LastModifiedDate'] = pd.to_datetime(df['LastModifiedDate'], errors='coerce', format='%m/%d/%Y')
26
 
27
+ # After converting, check if there are any missing dates (i.e., rows that failed to convert)
28
+ st.write("Missing date values after conversion:")
29
+ st.write(df[['Original Issue Date', 'Effective Date', 'Expiration Date', 'LastModifiedDate']].isnull().sum())
30
+
31
+ # Handling missing data selectively (only drop rows that lack critical columns like License Type)
32
+ df.dropna(subset=['License Type'], inplace=True) # Keep rows where 'License Type' is not missing
33
+ df.dropna(subset=['Original Issue Date'], inplace=True) # Drop rows where 'Original Issue Date' is missing
34
+
35
+ # Adding Year columns only if the date columns are not null
36
  df['Original Issue Year'] = df['Original Issue Date'].dt.year
37
  df['Effective Date Year'] = df['Effective Date'].dt.year
38
  df['Expiration Date Year'] = df['Expiration Date'].dt.year
39
  df['Last Modified Date Year'] = df['LastModifiedDate'].dt.year
40
 
41
+ # Debug: Check if the Year columns are populated
42
+ st.write("Year columns check:")
43
+ st.write(df[['Original Issue Year', 'Effective Date Year', 'Expiration Date Year', 'Last Modified Date Year']].head())
44
+
45
+ # Converting object columns to category (but only if they contain values)
46
  for col in df.select_dtypes(include='object').columns:
47
+ if df[col].notna().any():
48
+ df[col] = df[col].astype('category')
49
 
50
+ # Check the cleaned data
51
+ st.write("Cleaned data preview:")
52
+ st.write(df.head())
53
 
54
  # Visualization 1: Bar chart of licenses by type
55
  st.subheader("Licenses by Type")