jiyachachan moiralala commited on
Commit
98fa03f
·
verified ·
1 Parent(s): f86b6ac

Upload child mortality vs life expectancy.py (#10)

Browse files

- Upload child mortality vs life expectancy.py (58e9f1e5086f2d77125169b0998247a6768a271c)


Co-authored-by: Chenzhao Wang <[email protected]>

pages/1_Child Mortality VS Life Expectancy.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import streamlit as st
3
+ import altair as alt
4
+
5
+ child_mortality = pd.read_csv("https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/child_mortality_0_5_year_olds_dying_per_1000_born.csv")
6
+ life_expectancy = pd.read_csv("https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/life_expectancy.csv")
7
+
8
+ st.title("Child Mortality & Life Expectancy")
9
+
10
+ countries = list(child_mortality['country'].unique())
11
+ selected_countries = st.sidebar.multiselect("Select Countries", countries, default=["Argentina", "Australia", "China", "India", "South Africa", "UK", "USA"])
12
+ year_range = st.sidebar.slider("Select Year Range", 1900, 2024, (1900, 2024))
13
+
14
+ filtered_mortality = child_mortality[child_mortality['country'].isin(selected_countries)]
15
+ filtered_expectancy = life_expectancy[life_expectancy['country'].isin(selected_countries)]
16
+ years = [str(year) for year in range(year_range[0], year_range[1] + 1)]
17
+ filtered_mortality = filtered_mortality[['country'] + years]
18
+ filtered_expectancy = filtered_expectancy[['country'] + years]
19
+
20
+ def melt_dataframe(df, value_name):
21
+ df_melted = df.melt(id_vars=["country"], var_name="year", value_name=value_name)
22
+ df_melted["year"] = df_melted["year"].astype(int)
23
+ return df_melted
24
+
25
+ mortality_melted = melt_dataframe(filtered_mortality, "Child Mortality")
26
+ expectancy_melted = melt_dataframe(filtered_expectancy, "Life Expectancy")
27
+
28
+ # Chart 1: Child Mortality Trends
29
+ st.subheader("Chart 1: Child Mortality Trends")
30
+ st.write("""
31
+ This chart reveals significant regional disparities in child mortality rates across the
32
+ selected countries. For instance, India and South Africa initially exhibit much higher
33
+ mortality rates compared to developed nations like the USA and the UK, reflecting disparities
34
+ in healthcare access. Notably, the chart captures periods of global crises such as pandemics
35
+ or wars, where temporary spikes in child mortality rates can be observed, such as in South
36
+ Africa during the mid-20th century. Over time, all countries demonstrate a marked decline,
37
+ indicating progress in global health and development.
38
+ """)
39
+
40
+ mortality_chart = alt.Chart(mortality_melted).mark_line().encode(
41
+ x=alt.X("year:O", title="Year"),
42
+ y=alt.Y("Child Mortality:Q", title="Child Mortality (0–5 years per 1000 births)"),
43
+ color="country:N"
44
+ ).properties(width=700, height=400)
45
+
46
+ st.altair_chart(mortality_chart)
47
+
48
+ # Chart 2: Life Expectancy Trends
49
+ st.subheader("Chart 2: Life Expectancy Trends")
50
+ st.write("""
51
+ This visualization shows how life expectancy has improved across all regions, with
52
+ countries like the UK and the USA maintaining consistently higher life expectancy,
53
+ while developing nations such as India and South Africa only recently catching up.
54
+ However, fluctuations are visible, notably during the 1918 influenza pandemic and
55
+ the global conflicts of the 20th century, where life expectancy briefly plummeted.
56
+ Interestingly, the sharp rise in life expectancy in countries like China during the
57
+ mid-20th century reflects public health reforms and economic growth.
58
+ """)
59
+
60
+ expectancy_chart = alt.Chart(expectancy_melted).mark_line().encode(
61
+ x=alt.X("year:O", title="Year"),
62
+ y=alt.Y("Life Expectancy:Q", title="Life Expectancy at Birth"),
63
+ color="country:N"
64
+ ).properties(width=700, height=400)
65
+
66
+ st.altair_chart(expectancy_chart)
67
+
68
+ # Chart 3: Child Mortality vs. Life Expectancy
69
+ st.subheader("Chart 3: Child Mortality vs. Life Expectancy")
70
+ st.write("""
71
+ This scatter plot illustrates the strong inverse relationship between child mortality
72
+ rates and life expectancy at birth, underscoring how advancements in healthcare,
73
+ nutrition, and living conditions improve both indicators simultaneously. A particularly
74
+ striking observation is how the data for countries like India and South Africa forms a
75
+ steep curve, signifying rapid improvements in life expectancy as child mortality declines.
76
+ Developed nations such as Australia and the USA show a plateau in the later stages, where
77
+ child mortality rates are already low, and life expectancy improvements are incremental.
78
+ The trajectory of China’s data during the mid-20th century highlights a rapid transition,
79
+ likely due to systemic public health efforts.
80
+ """)
81
+
82
+ merged_data = pd.merge(mortality_melted, expectancy_melted, on=["country", "year"])
83
+ scatter_chart = alt.Chart(merged_data).mark_circle(size=60).encode(
84
+ x=alt.X("Life Expectancy:Q", title="Life Expectancy at Birth"),
85
+ y=alt.Y("Child Mortality:Q", title="Child Mortality (0–5 years per 1000 births)"),
86
+ color="country:N",
87
+ tooltip=["country", "year", "Child Mortality", "Life Expectancy"]
88
+ ).properties(width=700, height=400)
89
+
90
+ st.altair_chart(scatter_chart)