Spaces:
Runtime error
Runtime error
import pandas as pd | |
import streamlit as st | |
# Define the datasets as Python list dictionaries | |
natural_events = [ | |
{'year': 2011, 'location': 'Thailand', 'type': 'Flood', 'effect': 'Economic losses'}, | |
{'year': 2012, 'location': 'USA', 'type': 'Heat waves', 'effect': 'Crop yield reduction'}, | |
{'year': 2013, 'location': 'Philippines', 'type': 'Typhoon', 'effect': 'Death toll'}, | |
{'year': 2014, 'location': 'Brazil', 'type': 'Drought', 'effect': 'Water scarcity'}, | |
{'year': 2015, 'location': 'Indonesia', 'type': 'Forest fires', 'effect': 'Air pollution'}, | |
{'year': 2016, 'location': 'Russia', 'type': 'Melting permafrost', 'effect': 'Methane emissions'}, | |
{'year': 2017, 'location': 'USA', 'type': 'Hurricane', 'effect': 'Property damage'}, | |
{'year': 2018, 'location': 'Japan', 'type': 'Typhoon', 'effect': 'Economic losses'}, | |
{'year': 2019, 'location': 'Australia', 'type': 'Wildfires', 'effect': 'Temperature increase'}, | |
{'year': 2020, 'location': 'Brazil', 'type': 'Deforestation', 'effect': 'CO2 emissions'}, | |
] | |
population_growth = [ | |
{'year': 2011, 'country': 'India', 'global_population': 7061000000, 'growth_rate': 1.25}, | |
{'year': 2012, 'country': 'Ethiopia', 'global_population': 7096000000, 'growth_rate': 2.65}, | |
{'year': 2013, 'country': 'India', 'global_population': 7125000000, 'growth_rate': 1.26}, | |
{'year': 2014, 'country': 'Nigeria', 'global_population': 7152000000, 'growth_rate': 2.73}, | |
{'year': 2015, 'country': 'China', 'global_population': 7182000000, 'growth_rate': 0.52}, | |
{'year': 2016, 'country': 'India', 'global_population': 7214000000, 'growth_rate': 1.19}, | |
{'year': 2017, 'country': 'Nigeria', 'global_population': 7253000000, 'growth_rate': 2.61}, | |
{'year': 2018, 'country': 'India', 'global_population': 7291000000, 'growth_rate': 1.17}, | |
{'year': 2019, 'country': 'Pakistan', 'global_population': 7329000000, 'growth_rate': 2.04}, | |
{'year': 2020, 'country': 'Nigeria', 'global_population': 7366000000, 'growth_rate': 2.58}, | |
] | |
# Convert the datasets to Pandas DataFrames | |
natural_events_df = pd.DataFrame(natural_events) | |
population_growth_df = pd.DataFrame(population_growth) | |
# Find the top natural event for each year | |
top_event_df = natural_events_df.sort_values(by='year').groupby('year').first() | |
# Merge the population growth data with the top event data | |
merged_df = pd.merge(top_event_df, population_growth_df, on='year') | |
# Define a function to calculate the population increase for a given year and country | |
def calculate_population_increase(row): | |
population_increase = row['global_population'] * (row['growth_rate'] / 100) | |
return int(population_increase) | |
# Apply the population increase calculation to the merged data | |
merged_df['population_increase'] = merged_df.apply(calculate_population_increase, axis=1) | |
# Set up the Streamlit app | |
st.title("Top Natural Events and Population Growth") | |
# Display the merged data | |
st.write("Merged Data:") | |
st.write(merged_df) | |
# Perform a join to find the population growth for the top event in each year | |
top_event_population_growth = pd.merge(top_event_df, population_growth_df, on='year', how='left') | |
st.write("Population Growth for Top Events:") | |
st.write(top_event_population_growth) | |
# Display a bar chart of the population growth by year | |
population_chart = pd.DataFrame({'year': population_growth_df['year'], 'population_growth': population_growth_df['growth_rate']}) | |
population_chart.set_index('year', inplace=True) | |
st.write("Population Growth Chart:") | |
st.bar_chart(population_chart['population_growth']) | |
# Display a pie chart of the top event types | |
top_event_types = natural_events_df.groupby('type').size().reset_index(name='count') | |
st.write("Top Event Types:") | |
st.write(top_event_types) | |
st.write("Pie Chart of Top Event Types:") | |
st.plotly_chart(top_event_types, kind='pie', values='count', labels='type') | |
# Display a map of the event locations | |
st.write("Map of Event Locations:") | |
st.map(natural_events_df[['location']].drop_duplicates()) |