Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,65 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
# Display the first 5 rows of the dataset
|
| 11 |
-
st.write("First 5 rows of the dataset:")
|
| 12 |
-
st.write(df.head())
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
+
import altair as alt
|
| 5 |
+
|
| 6 |
+
# Load the datasets
|
| 7 |
+
child_mortality_path = "child_mortality.csv"
|
| 8 |
+
population_path = "pop.csv"
|
| 9 |
+
|
| 10 |
+
child_mortality = pd.read_csv(child_mortality_path)
|
| 11 |
+
population = pd.read_csv(population_path)
|
| 12 |
+
|
| 13 |
+
# Clean population data (handle k, M, B suffixes)
|
| 14 |
+
def convert_population(value):
|
| 15 |
+
if isinstance(value, str):
|
| 16 |
+
if 'B' in value:
|
| 17 |
+
return float(value.replace('B', '')) * 1_000_000_000
|
| 18 |
+
elif 'M' in value:
|
| 19 |
+
return float(value.replace('M', '')) * 1_000_000
|
| 20 |
+
elif 'k' in value:
|
| 21 |
+
return float(value.replace('k', '')) * 1_000
|
| 22 |
+
else:
|
| 23 |
+
return float(value)
|
| 24 |
+
return value
|
| 25 |
+
|
| 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 |
|
|
|
|
|
|
|
|
|