Shah-Miloni commited on
Commit
b396363
·
verified ·
1 Parent(s): f5dd5da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -18
app.py CHANGED
@@ -1,15 +1,14 @@
 
1
  import streamlit as st
2
  import pandas as pd
3
  import altair as alt
4
 
5
- # Load the datasets
6
- child_mortality_path = "child_mortality.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
- # Clean population data (handle k, M, B suffixes)
13
  def convert_population(value):
14
  if isinstance(value, str):
15
  if 'B' in value:
@@ -24,16 +23,13 @@ def convert_population(value):
24
 
25
  population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
26
 
27
- # Streamlit app title
28
  st.title("Child Mortality Rate vs Population")
29
 
30
- # Dropdown: Country selection
31
  st.subheader("Select a Country")
32
  countries = sorted(child_mortality['country'].unique())
33
  selected_country = st.selectbox("Country", countries, index=0)
34
 
35
  if selected_country:
36
- # Filter datasets for the selected country
37
  mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
38
  id_vars='country', var_name='year', value_name='child_mortality'
39
  )
@@ -41,19 +37,13 @@ if selected_country:
41
  id_vars='country', var_name='year', value_name='population'
42
  )
43
 
44
- # Merge datasets
45
  merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
46
  merged_country_data['year'] = merged_country_data['year'].astype(int)
47
-
48
- # Filter for 20-year intervals
49
  merged_country_data = merged_country_data[merged_country_data['year'] % 20 == 0]
50
-
51
- # Calculate Child Mortality Rate
52
  merged_country_data['child_mortality_rate'] = (
53
  merged_country_data['child_mortality'] * merged_country_data['population'] / 1_000
54
  )
55
 
56
- # Explanation of the visualization
57
  st.subheader("About the Visualization")
58
  st.write("""
59
  This visualization explores the trends in child mortality and population over time for a selected country.
@@ -69,7 +59,6 @@ if selected_country:
69
  - Data is shown in 20-year intervals for better clarity and simplicity.
70
  """)
71
 
72
- # Create chart
73
  chart = alt.Chart(merged_country_data).transform_fold(
74
  ['child_mortality_rate', 'population'],
75
  as_=['Metric', 'Value']
@@ -84,11 +73,7 @@ if selected_country:
84
  title=f"Child Mortality Rate and Population Trends in {selected_country} (Every 20 Years)"
85
  )
86
 
87
- # Display the chart
88
  st.altair_chart(chart, use_container_width=True)
89
-
90
- # Data preview
91
- st.subheader("Data Preview")
92
- st.write(merged_country_data)
93
  else:
94
  st.warning("Please select a country to view the trends.")
 
 
1
+ ```python
2
  import streamlit as st
3
  import pandas as pd
4
  import altair as alt
5
 
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
  def convert_population(value):
13
  if isinstance(value, str):
14
  if 'B' in value:
 
23
 
24
  population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
25
 
 
26
  st.title("Child Mortality Rate vs Population")
27
 
 
28
  st.subheader("Select a Country")
29
  countries = sorted(child_mortality['country'].unique())
30
  selected_country = st.selectbox("Country", countries, index=0)
31
 
32
  if selected_country:
 
33
  mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
34
  id_vars='country', var_name='year', value_name='child_mortality'
35
  )
 
37
  id_vars='country', var_name='year', value_name='population'
38
  )
39
 
 
40
  merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
41
  merged_country_data['year'] = merged_country_data['year'].astype(int)
 
 
42
  merged_country_data = merged_country_data[merged_country_data['year'] % 20 == 0]
 
 
43
  merged_country_data['child_mortality_rate'] = (
44
  merged_country_data['child_mortality'] * merged_country_data['population'] / 1_000
45
  )
46
 
 
47
  st.subheader("About the Visualization")
48
  st.write("""
49
  This visualization explores the trends in child mortality and population over time for a selected country.
 
59
  - Data is shown in 20-year intervals for better clarity and simplicity.
60
  """)
61
 
 
62
  chart = alt.Chart(merged_country_data).transform_fold(
63
  ['child_mortality_rate', 'population'],
64
  as_=['Metric', 'Value']
 
73
  title=f"Child Mortality Rate and Population Trends in {selected_country} (Every 20 Years)"
74
  )
75
 
 
76
  st.altair_chart(chart, use_container_width=True)
 
 
 
 
77
  else:
78
  st.warning("Please select a country to view the trends.")
79
+ ```