Spaces:
Sleeping
Sleeping
Update pages/extra.py
Browse files- pages/extra.py +50 -0
pages/extra.py
CHANGED
|
@@ -24,6 +24,56 @@ def convert_population(value):
|
|
| 24 |
population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
st.title("Child Mortality Rate vs Population")
|
| 28 |
# Dual-Y Axis Chart
|
| 29 |
dual_axis_chart = alt.layer(
|
|
|
|
| 24 |
population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
|
| 25 |
|
| 26 |
|
| 27 |
+
st.title("Child Mortality Rate vs Population")
|
| 28 |
+
|
| 29 |
+
st.write("""
|
| 30 |
+
This visualization explores the trends in child mortality and population over time for a selected country.
|
| 31 |
+
The data combines population estimates and child mortality rates (deaths per 1,000 live births)
|
| 32 |
+
into a unified scale by calculating the total child mortality as a proportion of the population.
|
| 33 |
+
|
| 34 |
+
""")
|
| 35 |
+
|
| 36 |
+
# Dropdown for Country Selection
|
| 37 |
+
st.subheader("Select a Country")
|
| 38 |
+
countries = sorted(child_mortality['country'].unique())
|
| 39 |
+
selected_country = st.selectbox("Country", countries, index=0)
|
| 40 |
+
|
| 41 |
+
if selected_country:
|
| 42 |
+
# Data Processing
|
| 43 |
+
mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
|
| 44 |
+
id_vars='country', var_name='year', value_name='child_mortality'
|
| 45 |
+
)
|
| 46 |
+
population_country = population[population['country'] == selected_country].melt(
|
| 47 |
+
id_vars='country', var_name='year', value_name='population'
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
|
| 51 |
+
merged_country_data['year'] = merged_country_data['year'].astype(int)
|
| 52 |
+
merged_country_data = merged_country_data[merged_country_data['year'] % 20 == 0]
|
| 53 |
+
merged_country_data['child_mortality_rate'] = (
|
| 54 |
+
merged_country_data['child_mortality'] * merged_country_data['population'] / 1_000
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Chart
|
| 58 |
+
chart = alt.Chart(merged_country_data).transform_fold(
|
| 59 |
+
['child_mortality_rate', 'population'],
|
| 60 |
+
as_=['Metric', 'Value']
|
| 61 |
+
).mark_line(point=True).encode(
|
| 62 |
+
x=alt.X('year:O', title='Year', axis=alt.Axis(labelAngle=0)),
|
| 63 |
+
y=alt.Y('Value:Q', title="Child Mortality Rate per Population", scale=alt.Scale(type='linear')),
|
| 64 |
+
color=alt.Color('Metric:N', title='Metrics', legend=alt.Legend(orient='top')),
|
| 65 |
+
tooltip=['year', 'Metric:N', 'Value:Q']
|
| 66 |
+
).properties(
|
| 67 |
+
width=800,
|
| 68 |
+
height=400,
|
| 69 |
+
title=f"Child Mortality Rate and Population Trends in {selected_country}"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
st.altair_chart(chart, use_container_width=True)
|
| 73 |
+
|
| 74 |
+
# Difficulties Section
|
| 75 |
+
|
| 76 |
+
|
| 77 |
st.title("Child Mortality Rate vs Population")
|
| 78 |
# Dual-Y Axis Chart
|
| 79 |
dual_axis_chart = alt.layer(
|