Spaces:
Runtime error
Runtime error
import streamlit as st | |
import plotly.express as px | |
import pandas as pd | |
# Define the states and conditions of interest with emojis | |
states = ["Minnesota 🌲", "Florida 🌴", "California 🌞"] | |
top_n = 10 | |
# Define the list dictionary of top 10 health conditions descending by cost with emojis | |
health_conditions = [ | |
{"condition": "Heart disease ❤️", "spending": 214.3}, | |
{"condition": "Trauma-related disorders 🤕", "spending": 198.6}, | |
{"condition": "Cancer 🦀", "spending": 171.0}, | |
{"condition": "Mental disorders 🧠", "spending": 150.8}, | |
{"condition": "Osteoarthritis and joint disorders 🦴", "spending": 142.4}, | |
{"condition": "Diabetes 🍬", "spending": 107.4}, | |
{"condition": "Chronic obstructive pulmonary disease and asthma 🫁", "spending": 91.0}, | |
{"condition": "Hypertension 🩺", "spending": 83.9}, | |
{"condition": "Hyperlipidemia 🍔", "spending": 83.9}, | |
{"condition": "Back problems 👨⚕️", "spending": 67.0} | |
] | |
# Total the spending values | |
total_spending = sum([hc["spending"] for hc in health_conditions]) | |
# Create a DataFrame from the list dictionary | |
df_top_conditions = pd.DataFrame(health_conditions) | |
# Create a new field showing the state with the most cases per year for each condition | |
df_top_conditions['most_cases_state'] = '' | |
for i, row in df_top_conditions.iterrows(): | |
condition = row['condition'].split(' ')[0] | |
condition_df = pd.read_csv(f'{condition}.csv') | |
condition_df = condition_df[condition_df['state'].isin(states)] | |
most_cases_state = condition_df.groupby('state')['cases'].sum().idxmax() | |
df_top_conditions.at[i, 'most_cases_state'] = f'{most_cases_state} 🏆' | |
# Create the treemap graph using Plotly Express | |
fig = px.treemap(df_top_conditions, path=["condition"], values="spending", color='most_cases_state') | |
# Set the title of the graph | |
fig.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)") | |
# Display the graph in Streamlit | |
st.plotly_chart(fig) | |