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 and Wikipedia links | |
health_conditions = [ | |
{ | |
"condition": "Heart disease ❤️", | |
"spending": 214.3, | |
"definition": "Heart disease is when your heart has problems working properly. It can cause chest pain and other symptoms. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Heart_disease)." | |
}, | |
{ | |
"condition": "Trauma-related disorders 🤕", | |
"spending": 198.6, | |
"definition": "Trauma-related disorders are mental health conditions that can occur after someone experiences a traumatic event, like a car accident or natural disaster. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Trauma_(medicine))." | |
}, | |
{ | |
"condition": "Cancer 🦀", | |
"spending": 171.0, | |
"definition": "Cancer is a disease where your body's cells grow out of control. It can cause lumps or tumors and make you feel sick. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Cancer)." | |
}, | |
{ | |
"condition": "Mental disorders 🧠", | |
"spending": 150.8, | |
"definition": "Mental disorders are conditions that affect your mood, behavior, or thinking. They can make it hard to function normally. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Mental_disorder)." | |
}, | |
{ | |
"condition": "Osteoarthritis and joint disorders 🦴", | |
"spending": 142.4, | |
"definition": "Osteoarthritis and joint disorders are conditions that can cause pain and stiffness in your joints. They often happen as you get older. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Osteoarthritis)." | |
}, | |
{ | |
"condition": "Diabetes 🍬", | |
"spending": 107.4, | |
"definition": "Diabetes is a condition where your body has trouble processing sugar. It can cause high blood sugar levels and make you feel sick. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Diabetes)." | |
}, | |
{ | |
"condition": "Chronic obstructive pulmonary disease and asthma 🫁", | |
"spending": 91.0, | |
"definition": "Chronic obstructive pulmonary disease (COPD) and asthma are conditions that can make it hard to breathe. They can cause coughing, wheezing, and other symptoms. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Chronic_obstructive_pulmonary_disease)." | |
}, | |
{ | |
"condition": "Hypertension 🩺", | |
"spending": 83.9, | |
"definition": "Hypertension is another word for high blood pressure. It can put you at risk for heart disease and other health problems. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Hypertension). | |
}, | |
{ | |
"condition": "Hyperlipidemia 🍔", | |
"spending": 83.9, | |
"definition": "Hyperlipidemia is a condition where you have high levels of fat in your blood. It can put you at risk for heart disease and other health problems. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Hyperlipidemia)." | |
}, | |
{ | |
"condition": "Back problems 👨⚕️", | |
"spending": 67.0, | |
"definition": "Back problems can cause pain and stiffness in your back. They can be caused by a variety of factors, like poor posture or injuries. Learn more at [Wikipedia](https://en.wikipedia.org/wiki/Back_pain)." | |
} | |
] | |
#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) | |
#Display definitions of each condition | |
st.markdown("## Condition Definitions") | |
for hc in health_conditions: | |
st.markdown(f"### {hc['condition']}") | |
st.markdown(hc['definition']) |