Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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
|
| 42 |
-
countries =
|
| 43 |
selected_country = st.sidebar.selectbox("Select Country", countries)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 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 |
-
#
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
st.
|
| 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.")
|