Spaces:
Sleeping
Sleeping
Rename pages/Child_mortality_rate_vs_Population.py to pages/Child Mortality Rate vs Population.py
Browse files
pages/{Child_mortality_rate_vs_Population.py → Child Mortality Rate vs Population.py}
RENAMED
|
@@ -2,12 +2,14 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import altair as alt
|
| 4 |
|
|
|
|
| 5 |
child_mortality_path = "child_mortality_0_5_year_olds_dying_per_1000_born.csv"
|
| 6 |
population_path = "pop.csv"
|
| 7 |
|
| 8 |
child_mortality = pd.read_csv(child_mortality_path)
|
| 9 |
population = pd.read_csv(population_path)
|
| 10 |
|
|
|
|
| 11 |
def convert_population(value):
|
| 12 |
if isinstance(value, str):
|
| 13 |
if 'B' in value:
|
|
@@ -22,13 +24,22 @@ def convert_population(value):
|
|
| 22 |
|
| 23 |
population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
|
| 24 |
|
|
|
|
| 25 |
st.title("Child Mortality Rate vs Population")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
st.subheader("Select a Country")
|
| 28 |
countries = sorted(child_mortality['country'].unique())
|
| 29 |
selected_country = st.selectbox("Country", countries, index=0)
|
| 30 |
|
| 31 |
if selected_country:
|
|
|
|
| 32 |
mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
|
| 33 |
id_vars='country', var_name='year', value_name='child_mortality'
|
| 34 |
)
|
|
@@ -39,39 +50,32 @@ if selected_country:
|
|
| 39 |
merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
|
| 40 |
merged_country_data['year'] = merged_country_data['year'].astype(int)
|
| 41 |
merged_country_data = merged_country_data[merged_country_data['year'] % 20 == 0]
|
| 42 |
-
merged_country_data['child_mortality_rate'] = (
|
| 43 |
-
merged_country_data['child_mortality'] * merged_country_data['population'] / 1_000
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
st.subheader("About the Visualization")
|
| 47 |
-
st.write("""
|
| 48 |
-
This visualization explores the trends in child mortality and population over time for a selected country.
|
| 49 |
-
The data combines population estimates and child mortality rates (deaths per 1,000 live births)
|
| 50 |
-
into a unified scale by calculating the total child mortality as a proportion of the population.
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
).properties(
|
| 70 |
-
|
| 71 |
-
height=400,
|
| 72 |
-
title=f"Child Mortality Rate and Population Trends in {selected_country} (Every 20 Years)"
|
| 73 |
)
|
| 74 |
|
| 75 |
-
st.altair_chart(
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
| 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:
|
|
|
|
| 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 |
)
|
|
|
|
| 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)
|
| 78 |
+
|
| 79 |
+
st.write("""
|
| 80 |
+
Initially, I planned to calculate the child mortality rate per population as a combined metric to represent both trends in one graph. However, this approach proved misleading because large populations could distort the results, masking the distinct trends of each metric. As a solution, I switched to a dual-axis chart, separating child mortality (per 1,000 live births) and population size (in millions) into independent axes. This made the comparison clearer, improved interpretability, and allowed for better interactivity and analysis of the two trends over time.
|
| 81 |
+
""")
|