GeekTony commited on
Commit
9473bb8
·
1 Parent(s): f70edb8

Update app.py

Browse files

Modified to display each of the last 10 years, using python code and data generated by ChatGPT

Files changed (1) hide show
  1. app.py +66 -15
app.py CHANGED
@@ -3,30 +3,81 @@ import streamlit as st
3
 
4
  # Define the datasets as Python list dictionaries
5
  natural_events = [
6
- {'location': 'Australia', 'type': 'Wildfires', 'year': 2019, 'effect': 'Temperature increase'},
7
- {'location': 'Brazil', 'type': 'Deforestation', 'year': 2020, 'effect': 'CO2 emissions'},
8
- {'location': 'Indonesia', 'type': 'Forest fires', 'year': 2015, 'effect': 'Air pollution'},
9
- {'location': 'USA', 'type': 'Heat waves', 'year': 2012, 'effect': 'Crop yield reduction'},
10
- {'location': 'Russia', 'type': 'Melting permafrost', 'year': 2016, 'effect': 'Methane emissions'}
 
 
 
 
 
11
  ]
12
 
13
  population_growth = [
14
- {'year': 2019, 'country': 'India', 'growth_rate': 1.08},
15
- {'year': 2020, 'country': 'Nigeria', 'growth_rate': 2.58},
16
- {'year': 2015, 'country': 'China', 'growth_rate': 0.48},
17
- {'year': 2012, 'country': 'Ethiopia', 'growth_rate': 2.89},
18
- {'year': 2016, 'country': 'India', 'growth_rate': 1.18}
 
 
 
 
 
19
  ]
20
 
21
  # Convert the datasets to Pandas DataFrames
22
  natural_events_df = pd.DataFrame(natural_events)
23
  population_growth_df = pd.DataFrame(population_growth)
24
 
25
- # Merge the two DataFrames on the year column
26
- merged_df = pd.merge(natural_events_df, population_growth_df, on='year')
27
 
28
- # Calculate the total population growth for each event and add it to the merged DataFrame
29
- merged_df['total_growth'] = merged_df['growth_rate'] * 1000000
30
 
31
- # Display the merged DataFrame in the Streamlit app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  st.write(merged_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # Define the datasets as Python list dictionaries
5
  natural_events = [
6
+ {'year': 2011, 'location': 'Thailand', 'type': 'Flood', 'effect': 'Economic losses'},
7
+ {'year': 2012, 'location': 'USA', 'type': 'Heat waves', 'effect': 'Crop yield reduction'},
8
+ {'year': 2013, 'location': 'Philippines', 'type': 'Typhoon', 'effect': 'Death toll'},
9
+ {'year': 2014, 'location': 'Brazil', 'type': 'Drought', 'effect': 'Water scarcity'},
10
+ {'year': 2015, 'location': 'Indonesia', 'type': 'Forest fires', 'effect': 'Air pollution'},
11
+ {'year': 2016, 'location': 'Russia', 'type': 'Melting permafrost', 'effect': 'Methane emissions'},
12
+ {'year': 2017, 'location': 'USA', 'type': 'Hurricane', 'effect': 'Property damage'},
13
+ {'year': 2018, 'location': 'Japan', 'type': 'Typhoon', 'effect': 'Economic losses'},
14
+ {'year': 2019, 'location': 'Australia', 'type': 'Wildfires', 'effect': 'Temperature increase'},
15
+ {'year': 2020, 'location': 'Brazil', 'type': 'Deforestation', 'effect': 'CO2 emissions'},
16
  ]
17
 
18
  population_growth = [
19
+ {'year': 2011, 'country': 'India', 'global_population': 7061000000, 'growth_rate': 1.25},
20
+ {'year': 2012, 'country': 'Ethiopia', 'global_population': 7096000000, 'growth_rate': 2.65},
21
+ {'year': 2013, 'country': 'India', 'global_population': 7125000000, 'growth_rate': 1.26},
22
+ {'year': 2014, 'country': 'Nigeria', 'global_population': 7152000000, 'growth_rate': 2.73},
23
+ {'year': 2015, 'country': 'China', 'global_population': 7182000000, 'growth_rate': 0.52},
24
+ {'year': 2016, 'country': 'India', 'global_population': 7214000000, 'growth_rate': 1.19},
25
+ {'year': 2017, 'country': 'Nigeria', 'global_population': 7253000000, 'growth_rate': 2.61},
26
+ {'year': 2018, 'country': 'India', 'global_population': 7291000000, 'growth_rate': 1.17},
27
+ {'year': 2019, 'country': 'Pakistan', 'global_population': 7329000000, 'growth_rate': 2.04},
28
+ {'year': 2020, 'country': 'Nigeria', 'global_population': 7366000000, 'growth_rate': 2.58},
29
  ]
30
 
31
  # Convert the datasets to Pandas DataFrames
32
  natural_events_df = pd.DataFrame(natural_events)
33
  population_growth_df = pd.DataFrame(population_growth)
34
 
35
+ # Find the top natural event for each year
36
+ top_event_df = natural_events_df.sort_values(by='year').groupby('year').first()
37
 
38
+ # Merge the population growth data with the top event data
39
+ merged_df = pd.merge(top_event_df, population_growth_df, on='year')
40
 
41
+ # Define a function to calculate the population increase for a given year and country
42
+ def calculate_population_increase(row):
43
+ population_increase = row['global_population'] * (row['growth_rate'] / 100)
44
+ return int(population_increase)
45
+
46
+ # Apply the population increase calculation to the merged data
47
+
48
+ merged_df['population_increase'] = merged_df.apply(calculate_population_increase, axis=1)
49
+
50
+ # Set up the Streamlit app
51
+
52
+ st.title("Top Natural Events and Population Growth")
53
+
54
+ # Display the merged data
55
+
56
+ st.write("Merged Data:")
57
  st.write(merged_df)
58
+
59
+ # Perform a join to find the population growth for the top event in each year
60
+
61
+ top_event_population_growth = pd.merge(top_event_df, population_growth_df, on='year', how='left')
62
+ st.write("Population Growth for Top Events:")
63
+ st.write(top_event_population_growth)
64
+
65
+ # Display a bar chart of the population growth by year
66
+
67
+ population_chart = pd.DataFrame({'year': population_growth_df['year'], 'population_growth': population_growth_df['growth_rate']})
68
+ population_chart.set_index('year', inplace=True)
69
+ st.write("Population Growth Chart:")
70
+ st.bar_chart(population_chart['population_growth'])
71
+
72
+ # Display a pie chart of the top event types
73
+
74
+ top_event_types = natural_events_df.groupby('type').size().reset_index(name='count')
75
+ st.write("Top Event Types:")
76
+ st.write(top_event_types)
77
+ st.write("Pie Chart of Top Event Types:")
78
+ st.plotly_chart(top_event_types, kind='pie', values='count', labels='type')
79
+
80
+ # Display a map of the event locations
81
+
82
+ st.write("Map of Event Locations:")
83
+ st.map(natural_events_df[['location']].drop_duplicates())