Spaces:
Sleeping
Sleeping
Create Child Mortality vs Population
#1
by
Shah-Miloni
- opened
pages/Child Mortality vs Population
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import altair as alt
|
| 4 |
+
|
| 5 |
+
# Load data
|
| 6 |
+
child_mortality_path = "child_mortality_0_5_year_olds_dying_per_1000_born.csv"
|
| 7 |
+
population_path = "pop.csv"
|
| 8 |
+
|
| 9 |
+
child_mortality = pd.read_csv(child_mortality_path)
|
| 10 |
+
population = pd.read_csv(population_path)
|
| 11 |
+
|
| 12 |
+
# Data Cleaning
|
| 13 |
+
def convert_population(value):
|
| 14 |
+
if isinstance(value, str):
|
| 15 |
+
if 'B' in value:
|
| 16 |
+
return float(value.replace('B', '')) * 1_000_000_000
|
| 17 |
+
elif 'M' in value:
|
| 18 |
+
return float(value.replace('M', '')) * 1_000_000
|
| 19 |
+
elif 'k' in value:
|
| 20 |
+
return float(value.replace('k', '')) * 1_000
|
| 21 |
+
else:
|
| 22 |
+
return float(value)
|
| 23 |
+
return value
|
| 24 |
+
|
| 25 |
+
population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
|
| 26 |
+
|
| 27 |
+
# Title and Description
|
| 28 |
+
st.title("Child Mortality Rate vs Population")
|
| 29 |
+
|
| 30 |
+
st.write("""
|
| 31 |
+
This visualization explores the relationship between child mortality rates (per 1,000 live births) and population size for a selected country over time.
|
| 32 |
+
By displaying both metrics side-by-side on a dual-y-axis chart, the visual aims to provide insights into how population trends and child mortality rates have evolved across decades.
|
| 33 |
+
The visualization is interactive, allowing users to select a country from the dropdown menu. This enables tailored exploration of trends specific to different countries.
|
| 34 |
+
Hovering over the chart provides tooltips with detailed information for each data point, including the year, population size, and child mortality rate.
|
| 35 |
+
""")
|
| 36 |
+
|
| 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 |
+
|
| 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 |
+
|
| 54 |
+
# Dual-Y Axis Chart
|
| 55 |
+
dual_axis_chart = alt.layer(
|
| 56 |
+
# First Layer: Child Mortality
|
| 57 |
+
alt.Chart(merged_country_data).mark_line(point=True).encode(
|
| 58 |
+
x=alt.X('year:O', title='Year (Every 20 Years)', axis=alt.Axis(labelAngle=0)),
|
| 59 |
+
y=alt.Y('child_mortality:Q', title='Child Mortality Rate (per 1,000 live births)', axis=alt.Axis(titleColor='lightblue')),
|
| 60 |
+
tooltip=['year', 'child_mortality']
|
| 61 |
+
).properties(
|
| 62 |
+
width=800,
|
| 63 |
+
height=400,
|
| 64 |
+
),
|
| 65 |
+
# Second Layer: Population
|
| 66 |
+
alt.Chart(merged_country_data).mark_line(color='orange', point=True).encode(
|
| 67 |
+
x=alt.X('year:O'),
|
| 68 |
+
y=alt.Y('population:Q', title='Population (in millions)', axis=alt.Axis(titleColor='orange')),
|
| 69 |
+
tooltip=['year', 'population']
|
| 70 |
+
)
|
| 71 |
+
).resolve_scale(
|
| 72 |
+
y='independent'
|
| 73 |
+
).properties(
|
| 74 |
+
title=f" Child Mortality and Population Trends in {selected_country}"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
st.altair_chart(dual_axis_chart, use_container_width=True)
|