johnbakerjr commited on
Commit
aefa26d
1 Parent(s): 97d64bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -63
app.py CHANGED
@@ -1,75 +1,18 @@
1
  import pandas as pd
2
- import numpy as np
3
-
4
- from dataprep.clean import clean_country
5
  import plotly.graph_objects as go
6
 
7
- gdp_per_capita = pd.read_csv('./data/country_gdp_per_capita_worldbank.csv')
8
- gdp_per_capita = gdp_per_capita[['Country Name', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']]
9
-
10
- usa = pd.read_csv('./data/USAData_RenewableInvestment_2010-2020.csv').set_index('Country')
11
-
12
- ## read plotly data ##
13
-
14
- temp_delta_plotly = pd.read_csv('./data/Annual_Surface_Temperature_Change.csv')
15
- invest_plotly = pd.read_csv('data/Environmental_Protection_Expenditures.csv')
16
- gdp_per_capita_plotly = gdp_per_capita.copy()
17
-
18
- ## prepare data for plotly ##
19
-
20
- gdp_per_capita_plotly = gdp_per_capita_plotly.rename(columns={'Country Name': 'Country'})
21
-
22
- # look up ISO3 codes for countries
23
- gdp_per_capita_plotly = clean_country(gdp_per_capita_plotly, "Country", output_format="alpha-3").rename(columns={'Country_clean': 'ISO3'})
24
- gdp_per_capita_plotly = gdp_per_capita_plotly[['Country', 'ISO3', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']]
25
- gdp_per_capita_plotly = gdp_per_capita_plotly.rename(columns={'2010': 2010, '2011': 2011, '2012': 2012, '2013': 2013, '2014': 2014, '2015': 2015, '2016': 2016, '2017': 2017, '2018': 2018, '2019': 2019, '2020': 2020})
26
-
27
- temp_delta_plotly = temp_delta_plotly[['Country', 'ISO3', 'F2010', 'F2011', 'F2012', 'F2013', 'F2014', 'F2015', 'F2016', 'F2017', 'F2018', 'F2019', 'F2020']]
28
- temp_delta_plotly = temp_delta_plotly.rename(columns={'F2010': 2010, 'F2011': 2011, 'F2012': 2012, 'F2013': 2013, 'F2014': 2014, 'F2015': 2015, 'F2016': 2016, 'F2017': 2017, 'F2018': 2018, 'F2019': 2019, 'F2020': 2020})
29
-
30
- usa2 = usa.copy().reset_index()
31
- usa2['ISO3'] = 'USA'
32
- invest_plotly = invest_plotly[invest_plotly['Unit'] == 'Percent of GDP'].fillna(int(0))
33
- invest_plotly = invest_plotly[['Country', 'ISO3', 'F2010', 'F2011', 'F2012', 'F2013', 'F2014', 'F2015', 'F2016', 'F2017', 'F2018', 'F2019', 'F2020']].reset_index(drop=True)
34
- invest_plotly = pd.concat([invest_plotly, usa2])
35
- invest_plotly = invest_plotly.groupby(['ISO3']).agg('sum').reset_index()
36
- invest_plotly = invest_plotly.rename(columns={'F2010': 2010, 'F2011': 2011, 'F2012': 2012, 'F2013': 2013, 'F2014': 2014, 'F2015': 2015, 'F2016': 2016, 'F2017': 2017, 'F2018': 2018, 'F2019': 2019, 'F2020': 2020})
37
 
38
- temp_delta_plotly = temp_delta_plotly.melt(id_vars=['ISO3'], value_vars=[2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020], var_name='Year', value_name='Temp_Change')
39
- gdp_per_capita_plotly = gdp_per_capita_plotly.melt(id_vars=['ISO3', 'Country'], value_vars=[2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020], var_name='Year', value_name='GDP_Per_Capita')
40
- invest_plotly = invest_plotly.melt(id_vars=['ISO3'], value_vars=[2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020], var_name='Year', value_name='Investment_Percent')
41
-
42
- plotly_data = pd.merge(temp_delta_plotly, invest_plotly, on=['ISO3', 'Year'])
43
- plotly_data = pd.merge(gdp_per_capita_plotly, plotly_data, on=['ISO3', 'Year'])
44
-
45
- # drop all YEMEN data, as there is missing GDP data and no temp_change data
46
- plotly_data = plotly_data.drop(plotly_data.loc[plotly_data['ISO3']=='YEM'].index)
47
-
48
- # drop 2010-2012 data for SOMALIA, as there is no GDP data
49
- plotly_data = plotly_data.drop(plotly_data.loc[(plotly_data['ISO3']=='SOM') & (plotly_data['Year']<2013)].index)
50
-
51
- new_country_data = plotly_data.copy()
52
- new_country_data = new_country_data.groupby('Country').agg({'Temp_Change': 'mean', 'Investment_Percent': 'mean', 'GDP_Per_Capita': 'mean'})
53
- new_country_data['Temp_Change'] = new_country_data['Temp_Change'] + .25
54
- new_country_data = new_country_data.rename(columns={'Temp_Change': 'temp_delta_avg', 'Investment_Percent': 'renew_invest_avg', 'GDP_Per_Capita': 'gdp_per_capita_avg'})
55
 
 
56
  g20 = ['Argentina', 'Australia', 'Brazil', 'Canada', 'China', 'France', 'Germany', 'India', 'Indonesia', 'Italy', 'Japan', 'Republic of Korea', 'Mexico', 'Russia', 'Saudi Arabia', 'South Africa', 'Turkey', 'United Kingdom', 'United States', 'Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'Greece', 'Hungary', 'Ireland', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain', 'Sweden']
57
- new_country_data['g20'] = new_country_data.index.isin(g20).tolist()
58
-
59
- g20_countries = new_country_data.loc[new_country_data['g20'] == True].index.to_list()
60
 
61
 
62
- plotly_data['Temp_Change'] = plotly_data['Temp_Change'] + .25
63
-
64
- p1 = (plotly_data['Investment_Percent'] > 2)
65
- p2 = (plotly_data['Temp_Change'] < 1.5)
66
-
67
- plotly_data['color_code'] = np.where(p1 & p2, '#46725D', "False")
68
- plotly_data['color_code'] = np.where(p1 & ~p2, '#A46D13', plotly_data['color_code'])
69
- plotly_data['color_code'] = np.where(~p1 & p2, '#505693', plotly_data['color_code'])
70
- plotly_data['color_code'] = np.where(~p1 & ~p2, '#9A381D', plotly_data['color_code'])
71
-
72
  # make plotly figure
 
73
 
74
  dataset = plotly_data.copy()
75
 
 
1
  import pandas as pd
 
 
 
2
  import plotly.graph_objects as go
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # read data
6
+ plotly_data = pd.read_csv('plotly_data.csv')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # find countries in G20
9
  g20 = ['Argentina', 'Australia', 'Brazil', 'Canada', 'China', 'France', 'Germany', 'India', 'Indonesia', 'Italy', 'Japan', 'Republic of Korea', 'Mexico', 'Russia', 'Saudi Arabia', 'South Africa', 'Turkey', 'United Kingdom', 'United States', 'Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'Greece', 'Hungary', 'Ireland', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain', 'Sweden']
10
+ plotly_data['g20'] = plotly_data['Country'].isin(g20).tolist()
11
+ g20_countries = plotly_data.loc[plotly_data['g20'] == True]['Country'].to_list()
 
12
 
13
 
 
 
 
 
 
 
 
 
 
 
14
  # make plotly figure
15
+ import plotly.graph_objects as go
16
 
17
  dataset = plotly_data.copy()
18