Skip to content

Commit

Permalink
updated column and filter names for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyschenck authored Oct 14, 2023
1 parent 59538a3 commit 621f85e
Showing 1 changed file with 61 additions and 21 deletions.
82 changes: 61 additions & 21 deletions streamlit-data-aggviz.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#TO RUN, AFTER SAVING FILE WITH NAME 'streamlit-data-aggviz.py' GO TO TERMINAL OR POWERSHELL AND RUN COMMAND: streamlit run streamlit-data-aggviz.py
#GO TO POWERSHELL AND RUN COMMAND: streamlit run streamlit-data-aggviz.py

import streamlit as st
import os
import pandas as pd


st.set_page_config(layout="wide")

st.title("Data Viewer")
st.title("Sacramento Campaign Finance 2014 - 2023")

folder_path = 'data/campaign_finance'
folder_path = 'data/'

# Function to read all JSON files from a folder into a single DataFrame
def read_jsons_from_folder(folder_path):
Expand All @@ -30,27 +31,53 @@ def read_jsons_from_folder(folder_path):
combined_df['Campaigns-allYears'] = combined_df['filerName'].str[:-5] \
.where(combined_df['filerName'].str[-4:].str.isnumeric(), combined_df['filerName'])

# Replace 'City Council' values
# Replace 'City Council' label for 2023 data with general "Sacramento City" label used for all previous years
combined_df['agencyName'] = combined_df['agencyName'].replace("City Council", "Sacramento City")

# Replace 'Board of Supervisors' label for 2023 data with general "Sacramento County" label used for all previous years
combined_df['agencyName'] = combined_df['agencyName'].replace("Board of Supervisors", "Sacramento County")

# convert 'year' column to string
combined_df['year'] = combined_df['year'].astype(str)

original_df = combined_df[['agencyName', 'filerName', 'Campaigns-allYears', 'amount', 'year', 'date', 'contributorLastName', 'contributorFirstName', 'contributorCity', 'contributorZip', 'contributorOccupation', 'contributorEmployer','committeeType','transactionId']]


# Initial filter options with 'All'
filters = {
'year': 'All',
'agencyName': 'All',
'Campaigns-allYears':'All',
'filerName': 'All',
'contributorLastName': 'All',
'contributorCity': 'All',
'contributorZip': 'All',
'contributorOccupation': 'All',
'contributorEmployer': 'All'
}
# Create a dictionary to map old column names to new column names
column_mapping = {'agencyName':'Entity',
'filerName':'Campaign/PAC',
'Campaigns-allYears':'Campaigns/PACs-all years',
'amount':'Contribution',
'year':'Year',
'date':'Date',
'contributorLastName':'Contributor Last Name',
'contributorFirstName':'Contributor First Name',
'contributorCity':'Contributor City',
'contributorZip':'Contributor Zip',
'contributorOccupation':'Contributor Occupation',
'contributorEmployer':'Contributor Employer',
'committeeType':'Committee Type',
'transactionId':'Transaction ID #',
}

# Rename the columns using the rename method
original_df.rename(columns=column_mapping, inplace=True)


filters = {'Entity': 'All',
'Campaign/PAC': 'All',
'Campaigns/PACs-all years': 'All',
'Contribution': 'All',
'Year': 'All',
'Date': 'All',
"Contributor Last Name": 'All',
"Contributor First Name": 'All',
'Contributor City': 'All',
'Contributor Zip': 'All',
'Contributor Occupation': 'All',
'Contributor Employer': 'All',
'Committee Type': 'All',
'Transaction ID #': 'All',
}

# Create a function to get the unique values for a column based on active filters
def get_filtered_values(column, active_filters):
Expand All @@ -71,16 +98,29 @@ def get_filtered_values(column, active_filters):
filtered_df = filtered_df[filtered_df[key] == (int(value) if key == 'year' else value)]

# Display aggregate total of 'amount'
aggregate_total = filtered_df['amount'].sum()
aggregate_total = filtered_df['Contribution'].sum()
st.write(f"Aggregate Total Amount: ${aggregate_total:,.2f}")
st.write(filtered_df)

# Bar Chart: Amount by user-selected category
categories = ['agencyName', 'Campaigns-allYears','filerName', 'contributorLastName', 'contributorFirstName', 'contributorCity', 'contributorZip', 'contributorOccupation', 'contributorEmployer']
categories = ['Entity',
'Campaign/PAC',
'Campaigns/PACs-all years',
'Contribution',
'Year',
'Date',
"Contributor Last Name",
"Contributor First Name",
'Contributor City',
'Contributor Zip',
'Contributor Occupation',
'Contributor Employer',
'Committee Type',
'Transaction ID #']
selected_category = st.selectbox("Select a category for the bar chart", categories)
amount_by_category = filtered_df.groupby(selected_category)['amount'].sum()
amount_by_category = filtered_df.groupby(selected_category)['Contribution'].sum()
st.bar_chart(amount_by_category)

# Line Chart: Aggregated amount by year
amount_by_year = filtered_df.groupby('year')['amount'].sum()
amount_by_year = filtered_df.groupby('Year')['Contribution'].sum()
st.line_chart(amount_by_year)

0 comments on commit 621f85e

Please sign in to comment.