Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import streamlit as st
- import polars as pl
- import pandas as pd
- import numpy as np
- import plotly.express as px
- import sys
- st.set_page_config(page_title="Election Party Candidate Analysis",
- layout='wide',
- initial_sidebar_state="expanded")
- @st.cache_data
- def get_data():
- df = pl.scan_parquet('Elections_Data_Compiled.parquet')
- return df.fetch(n_rows=sys.maxsize).to_pandas()
- df = get_data()
- header_left,header_mid,header_right = st.columns([1,6,1],gap = "large")
- with header_mid:
- # https://docs.streamlit.io/library/get-started/create-an-app
- st.title("Party Criminal Records Analysis")
- with st.sidebar:
- State_List = df["State"].unique().tolist()
- State_Selected = st.multiselect(label="Select State",
- options = State_List,
- default = "Uttar Pradesh", # Delhi West Bengal
- max_selections=1
- )
- if State_Selected:
- Year_List = df['Year'].sort_values().unique().tolist()
- Year_Selected = st.multiselect(label="Select Election Year",
- options=Year_List,
- default=Year_List[-1])
- if State_Selected and Year_Selected:
- df_selected = (df.loc[(df['State'].isin(State_Selected))
- & (df['Year'].isin(Year_Selected))]
- )
- df_18 = (df_selected.groupby('Party', as_index=False)['Criminal_Case'].sum()
- .sort_values(by="Criminal_Case", ascending=False)
- .head(18))
- if not df_18.empty:
- fig_party_crime_sum = px.bar(
- df_18,
- orientation='h',
- x='Criminal_Case',y='Party', color="Party",
- labels={"Criminal_Case":"Total Criminal Cases",
- "Party": "Election Parties"
- },
- title = ("<b>{}</b> - Top 18 Election Parties with Total Criminal Records in "
- "{} Elections").format(State_Selected, Year_Selected))
- fig_party_crime_sum.update_layout(title_font_size=26, height = 600,
- showlegend=False
- )
- fig_party_crime_sum.add_annotation(
- showarrow=False,
- text='Data Source: https://myneta.info/',
- xanchor='right',
- x=2,
- xshift=675,
- yanchor='bottom',
- y=0.01,
- )
- st.plotly_chart(fig_party_crime_sum,use_container_width=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement