Shah-Miloni commited on
Commit
58f8384
·
verified ·
1 Parent(s): 0cecf23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -31
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import streamlit as st
3
  import pandas as pd
4
  import altair as alt
@@ -26,40 +25,45 @@ def convert_population(value):
26
  population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
27
 
28
  # Streamlit app title
29
- st.title("Child Mortality vs Population Across Years")
30
-
31
- # Sidebar: Year selection
32
- year = st.sidebar.slider("Select Year", min_value=1800, max_value=2020, value=2020, step=1)
33
-
34
- # Filter datasets for the selected year
35
- mortality_year = child_mortality[['country', str(year)]].rename(columns={str(year): 'child_mortality'})
36
- population_year = population[['country', str(year)]].rename(columns={str(year): 'population'})
37
-
38
- # Merge datasets
39
- merged_data = pd.merge(mortality_year, population_year, on='country', how='inner').dropna()
40
 
41
- # Sidebar: Country dropdown
42
- countries = ['All'] + sorted(merged_data['country'].unique())
43
  selected_country = st.sidebar.selectbox("Select Country", countries)
44
 
45
- if selected_country != 'All':
46
- merged_data = merged_data[merged_data['country'] == selected_country]
 
 
 
 
 
 
47
 
48
- # Scatter plot
49
- scatter_chart = alt.Chart(merged_data).mark_circle(size=60).encode(
50
- x=alt.X('population', title='Population'),
51
- y=alt.Y('child_mortality', title='Child Mortality Rate'),
52
- tooltip=['country', 'population', 'child_mortality']
53
- ).properties(
54
- width=700,
55
- height=400,
56
- title=f"Child Mortality vs Population in {year}"
57
- )
58
 
59
- # Display chart
60
- st.altair_chart(scatter_chart, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- # Data preview
63
- st.subheader("Data Preview")
64
- st.write(merged_data)
65
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import altair as alt
 
25
  population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
26
 
27
  # Streamlit app title
28
+ st.title("Country Trends: Child Mortality and Population Over Time")
 
 
 
 
 
 
 
 
 
 
29
 
30
+ # Sidebar: Country selection
31
+ countries = sorted(child_mortality['country'].unique())
32
  selected_country = st.sidebar.selectbox("Select Country", countries)
33
 
34
+ # Filter datasets for the selected country
35
+ if selected_country:
36
+ mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
37
+ id_vars='country', var_name='year', value_name='child_mortality'
38
+ )
39
+ population_country = population[population['country'] == selected_country].melt(
40
+ id_vars='country', var_name='year', value_name='population'
41
+ )
42
 
43
+ # Merge datasets
44
+ merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
45
+ merged_country_data['year'] = merged_country_data['year'].astype(int)
 
 
 
 
 
 
 
46
 
47
+ # Line chart for trends
48
+ line_chart = alt.Chart(merged_country_data).transform_fold(
49
+ fold=['child_mortality', 'population'],
50
+ as_=['Metric', 'Value']
51
+ ).mark_line().encode(
52
+ x=alt.X('year:O', title='Year'),
53
+ y=alt.Y('Value:Q', title='Value', scale=alt.Scale(type='log')),
54
+ color=alt.Color('Metric:N', title='Metric'),
55
+ tooltip=['year', 'child_mortality', 'population']
56
+ ).properties(
57
+ width=700,
58
+ height=400,
59
+ title=f"Trends in Child Mortality and Population for {selected_country}"
60
+ )
61
 
62
+ # Display chart
63
+ st.altair_chart(line_chart, use_container_width=True)
 
64
 
65
+ # Data preview
66
+ st.subheader("Data Preview")
67
+ st.write(merged_country_data)
68
+ else:
69
+ st.warning("Please select a country to view the trends.")