Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2023
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import streamlit as st
  2. import polars as pl
  3. import pandas as pd
  4. import numpy as np
  5. import plotly.express as px
  6. import sys
  7.  
  8.  
  9. st.set_page_config(page_title="Election Party Candidate Analysis",
  10. layout='wide',
  11. initial_sidebar_state="expanded")
  12.  
  13.  
  14. @st.cache_data
  15. def get_data():
  16. df = pl.scan_parquet('Elections_Data_Compiled.parquet')
  17.  
  18. return df.fetch(n_rows=sys.maxsize).to_pandas()
  19.  
  20. df = get_data()
  21.  
  22. header_left,header_mid,header_right = st.columns([1,6,1],gap = "large")
  23.  
  24. with header_mid:
  25. # https://docs.streamlit.io/library/get-started/create-an-app
  26. st.title("Party Criminal Records Analysis")
  27.  
  28.  
  29. with st.sidebar:
  30. State_List = df["State"].unique().tolist()
  31.  
  32. State_Selected = st.multiselect(label="Select State",
  33. options = State_List,
  34. default = "Uttar Pradesh", # Delhi West Bengal
  35. max_selections=1
  36. )
  37.  
  38. if State_Selected:
  39. Year_List = df['Year'].sort_values().unique().tolist()
  40. Year_Selected = st.multiselect(label="Select Election Year",
  41. options=Year_List,
  42. default=Year_List[-1])
  43.  
  44. if State_Selected and Year_Selected:
  45. df_selected = (df.loc[(df['State'].isin(State_Selected))
  46. & (df['Year'].isin(Year_Selected))]
  47. )
  48.  
  49. df_18 = (df_selected.groupby('Party', as_index=False)['Criminal_Case'].sum()
  50. .sort_values(by="Criminal_Case", ascending=False)
  51. .head(18))
  52.  
  53. if not df_18.empty:
  54.  
  55. fig_party_crime_sum = px.bar(
  56. df_18,
  57. orientation='h',
  58. x='Criminal_Case',y='Party', color="Party",
  59. labels={"Criminal_Case":"Total Criminal Cases",
  60. "Party": "Election Parties"
  61. },
  62.  
  63. title = ("<b>{}</b> - Top 18 Election Parties with Total Criminal Records in "
  64. "{} Elections").format(State_Selected, Year_Selected))
  65.  
  66. fig_party_crime_sum.update_layout(title_font_size=26, height = 600,
  67. showlegend=False
  68. )
  69. fig_party_crime_sum.add_annotation(
  70. showarrow=False,
  71. text='Data Source: https://myneta.info/',
  72. xanchor='right',
  73. x=2,
  74. xshift=675,
  75. yanchor='bottom',
  76. y=0.01,
  77. )
  78.  
  79. st.plotly_chart(fig_party_crime_sum,use_container_width=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement