Shah-Miloni commited on
Commit
339fd06
·
verified ·
1 Parent(s): 73d9825

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -15
app.py CHANGED
@@ -28,21 +28,6 @@ st.subheader("Select a Country")
28
  countries = sorted(child_mortality['country'].unique())
29
  selected_country = st.selectbox("Country", countries, index=0)
30
 
31
- st.subheader("About the Visualization")
32
- st.write("""
33
- This visualization explores the trends in child mortality and population over time for a selected country.
34
- The data combines population estimates and child mortality rates (deaths per 1,000 live births)
35
- into a unified scale by calculating the total child mortality as a proportion of the population.
36
-
37
- **Scaling Explanation**:
38
- - The child mortality rate is computed using the formula:
39
- \[
40
- \\text{Child Mortality Rate} = \\frac{\\text{Child Mortality (per 1,000 live births)} \\times \\text{Population}}{1,000}
41
- \]
42
- - Both metrics are plotted on the same y-axis to enable direct comparison, providing a holistic view of population and child mortality trends.
43
- - Data is shown in 20-year intervals for better clarity and simplicity.
44
- """)
45
-
46
  if selected_country:
47
  mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
48
  id_vars='country', var_name='year', value_name='child_mortality'
@@ -52,4 +37,41 @@ if selected_country:
52
  )
53
 
54
  merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
 
 
 
 
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'
 
37
  )
38
 
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
+ **Scaling Explanation**:
53
+ - The child mortality rate is computed using the formula:
54
+ \[
55
+ \\text{Child Mortality Rate} = \\frac{\\text{Child Mortality (per 1,000 live births)} \\times \\text{Population}}{1,000}
56
+ \]
57
+ - Both metrics are plotted on the same y-axis to enable direct comparison, providing a holistic view of population and child mortality trends.
58
+ - Data is shown in 20-year intervals for better clarity and simplicity.
59
+ """)
60
+
61
+ chart = alt.Chart(merged_country_data).transform_fold(
62
+ ['child_mortality_rate', 'population'],
63
+ as_=['Metric', 'Value']
64
+ ).mark_line(point=True).encode(
65
+ x=alt.X('year:O', title='Year (Every 20 Years)', axis=alt.Axis(labelAngle=0)),
66
+ y=alt.Y('Value:Q', title='Value (Scaled)', scale=alt.Scale(type='linear')),
67
+ color=alt.Color('Metric:N', title='Metrics', legend=alt.Legend(orient='top')),
68
+ tooltip=['year', 'Metric:N', 'Value:Q']
69
+ ).properties(
70
+ width=800,
71
+ height=400,
72
+ title=f"Child Mortality Rate and Population Trends in {selected_country} (Every 20 Years)"
73
+ )
74
 
75
+ st.altair_chart(chart, use_container_width=True)
76
+ else:
77
+ st.warning("Please select a country to view the trends.")