Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import random | |
import plotly.graph_objects as go | |
import plotly.express as px | |
# Define the states and conditions of interest | |
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", "emoji": "π", "spending": 214.3}, | |
{"condition": "π€ Trauma-related disorders", "emoji": "π", "spending": 198.6}, | |
{"condition": "π¦ Cancer", "emoji": "ποΈ", "spending": 171.0}, | |
{"condition": "π§ Mental disorders", "emoji": "π§", "spending": 150.8}, | |
{"condition": "𦴠Osteoarthritis and joint disorders", "emoji": "π₯", "spending": 142.4}, | |
{"condition": "π Diabetes", "emoji": "π©Έ", "spending": 107.4}, | |
{"condition": "π« Chronic obstructive pulmonary disease and asthma", "emoji": "π«", "spending": 91.0}, | |
{"condition": "π©Ί Hypertension", "emoji": "π", "spending": 83.9}, | |
{"condition": "π¬ Hyperlipidemia", "emoji": "π¬", "spending": 83.9}, | |
{"condition": "𦴠Back problems", "emoji": "π§", "spending": 67.0} | |
] | |
# Create a DataFrame from the list dictionary | |
df_top_conditions = pd.DataFrame(health_conditions) | |
# Calculate the total spending | |
total_spending = round(df_top_conditions["spending"].sum(), 1) | |
# Define the roll function | |
def roll(): | |
# Generate a thousand 10-sided dice rolls for each health condition | |
rolls = [random.randint(1, 10) for _ in range(1000)] | |
# Calculate the frequency of each variant | |
frequencies = [rolls.count(i) for i in range(1, 11)] | |
return frequencies | |
# Define the sunburst chart | |
fig_sunburst = go.Figure(go.Sunburst( | |
labels=df_top_conditions["emoji"] + " " + df_top_conditions["condition"], | |
parents=[""] * top_n, | |
values=df_top_conditions["spending"], | |
maxdepth=2 | |
)) | |
# Customize the layout of the sunburst chart | |
fig_sunburst.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)") | |
# Display the sunburst chart and variants per condition in the Streamlit app | |
st.plotly_chart(fig_sunburst) | |
for index, row in df_top_conditions.iterrows(): | |
frequencies = roll() | |
fig_bar = px.bar(x=[f"Variant {i}" for i in range(1, 11)], y=frequencies[:10], labels={'x': 'Variant', 'y': 'Frequency'}) | |
fig_bar.update_layout(title=f"Variants of {row['condition']} ({row['emoji']})") | |
st.plotly_chart(fig_bar) | |