import streamlit as st import pandas as pd import altair as alt # Load hospital dataset hospitals_df = pd.read_csv("https://data.medicare.gov/api/views/xubh-q36u/rows.csv?accessType=DOWNLOAD") # Filter for the largest hospital in each state largest_hospitals_df = hospitals_df.loc[hospitals_df.groupby("State")["Hospital overall rating"].idxmax()] # Select columns to display cols_to_display = ["State", "Hospital Name", "City", "Zip Code", "lat", "lng"] # Create a Streamlit table to display the largest hospitals st.table(largest_hospitals_df[cols_to_display]) # Define chart functions def stacked_bar_chart(): chart = alt.Chart(largest_hospitals_df).mark_bar().encode( x=alt.X('State:N'), y=alt.Y('count():Q', stack="normalize"), color=alt.Color('Hospital Type:N'), tooltip=['Hospital Name', 'City', 'Hospital overall rating'] ).properties( width=700, height=400, title='Number of Hospitals by State and Type' ) text = chart.mark_text( align='center', baseline='middle', dx=0, dy=5, color='white' ).encode( text=alt.Text('count():Q', format='.1f') ) st.altair_chart(chart + text) def bump_chart(): chart = alt.Chart(largest_hospitals_df).transform_rank( 'Hospital overall rating', groupby=['State'] ).mark_line().encode( x='rank:Q', y=alt.Y('Hospital Name:N', sort='-x'), color=alt.Color('State:N'), tooltip=['City', 'Zip Code', 'Hospital overall rating'] ).properties( width=700, height=400, title='Hospital Rankings by State' ) st.altair_chart(chart) # Define chart buttons st.sidebar.header('Select a Chart') chart_options = ['Stacked Bar Chart with Text Overlay', 'Bump Chart'] chart_choice = st.sidebar.selectbox('', chart_options) # Call chart functions based on user input if chart_choice == 'Stacked Bar Chart with Text Overlay': stacked_bar_chart() elif chart_choice == 'Bump Chart': bump_chart()