File size: 3,560 Bytes
9829548
 
0cecf23
 
b396363
0cecf23
 
 
 
 
14b3cd6
0cecf23
 
 
 
 
 
 
 
 
 
 
 
 
 
14b3cd6
9cb8281
0cecf23
14b3cd6
 
 
 
 
 
 
 
f5dd5da
58f8384
f5dd5da
0cecf23
58f8384
14b3cd6
58f8384
 
 
 
 
 
0cecf23
73d9825
339fd06
 
 
 
 
 
14b3cd6
339fd06
 
 
 
da1cf6c
 
339fd06
 
 
 
 
da1cf6c
339fd06
73d9825
339fd06
14b3cd6
 
 
 
 
 
 
 
 
 
339fd06
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import streamlit as st
import pandas as pd
import altair as alt

child_mortality_path = "child_mortality_0_5_year_olds_dying_per_1000_born.csv"
population_path = "pop.csv"

child_mortality = pd.read_csv(child_mortality_path)
population = pd.read_csv(population_path)

# Data Cleaning
def convert_population(value):
    if isinstance(value, str):
        if 'B' in value:
            return float(value.replace('B', '')) * 1_000_000_000
        elif 'M' in value:
            return float(value.replace('M', '')) * 1_000_000
        elif 'k' in value:
            return float(value.replace('k', '')) * 1_000
        else:
            return float(value)
    return value

population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)


st.title("Child Mortality Rate vs Population")

st.write("""
    This visualization explores the trends in child mortality and population over time for a selected country. 
    The data combines population estimates and child mortality rates (deaths per 1,000 live births) 
    into a unified scale by calculating the total child mortality as a proportion of the population. 
    
""")

# Dropdown for Country Selection
st.subheader("Select a Country")
countries = sorted(child_mortality['country'].unique())
selected_country = st.selectbox("Country", countries, index=0)

if selected_country:
    # Data Processing
    mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
        id_vars='country', var_name='year', value_name='child_mortality'
    )
    population_country = population[population['country'] == selected_country].melt(
        id_vars='country', var_name='year', value_name='population'
    )

    merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
    merged_country_data['year'] = merged_country_data['year'].astype(int)
    merged_country_data = merged_country_data[merged_country_data['year'] % 20 == 0]
    merged_country_data['child_mortality_rate'] = (
        merged_country_data['child_mortality'] * merged_country_data['population'] / 1_000
    )

    # Chart
    chart = alt.Chart(merged_country_data).transform_fold(
        ['child_mortality_rate', 'population'],
        as_=['Metric', 'Value']
    ).mark_line(point=True).encode(
        x=alt.X('year:O', title='Year', axis=alt.Axis(labelAngle=0)),
        y=alt.Y('Value:Q', title="Child Mortality Rate per Population", scale=alt.Scale(type='linear')),
        color=alt.Color('Metric:N', title='Metrics', legend=alt.Legend(orient='top')),
        tooltip=['year', 'Metric:N', 'Value:Q']
    ).properties(
        width=800,
        height=400,
        title=f"Child Mortality Rate and Population Trends in {selected_country}"
    )

    st.altair_chart(chart, use_container_width=True)

    # Difficulties Section
    st.subheader("Challenges Faced")
    st.write("""While building the visualization I began by trying to scale the data for the y-axis. Having both metrics on the same axis would enable direct comparison, providing a holistic view of population and child mortality trends.
    - To do that I calculated the child mortality rate:  
    - {Child Mortality Rate} = {{Child Mortality(per 1,000 live births)}*{Population}}/{1,000}
    - Data is shown in 20-year intervals for better clarity and simplicity.
    - Population data was stored with suffixes ('M', 'B', 'k'), which required conversion to numeric formats for proper calculations.

    """)
else:
    st.warning("Please select a country to view the trends.")