-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
146 lines (100 loc) · 4.11 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Python script to wrangle data to create the json file.
# Load
import pandas as pd
import numpy as np
import json
import geojson
import urllib.request
with urllib.request.urlopen("https://raw.githubusercontent.com/ctedja/assets/main/ne_50m_admin_0_countries.geojson") as url:
data = json.load(url)
# For full head viewing options.
# pd.options.display.max_columns = None
# pd.options.display.max_rows = None
summaries = pd.read_excel('data_entry.xlsx', 'summaries')
indicators = pd.read_excel('data_entry.xlsx', 'indicators')
mvam = pd.read_excel('data_entry.xlsx', 'mvam')
indicators.to_csv("temp1.csv", index=False, encoding='utf8')
# Inspect
# summaries.describe()
# summaries.info()
# summaries.id.head()
# Join
df = summaries.merge(indicators,
how='left',
left_on='id',
right_on='ID')
df = df.drop(['ID', 'Country'], axis=1)
df[['Overall Vulnerability', 'id']]
indicators[['Overall Vulnerability', 'ID']]
colIds = list(df.columns[list(range(0, 20))])
colVals = list(df.columns[list(range(20, 30))])
# Pivot Longer
df = pd.melt(df,
id_vars=colIds,
value_vars=colVals,
var_name="indicator")
df['indicator'].unique()
# Create a new column and use np.select to assign values to it using our lists as arguments
conditions = [
(df['value'] == 'Country of Concern'),
(df['value'] == 'High'),
(df['value'] == 'Country to Monitor'),
(df['value'] == 'Moderate'),
(df['value'] == 'Lower Vulnerability'),
(df['value'] == 'Low'),
(df['value'] == None)
]
values = [1, 1, 2, 2, 3, 3, None]
df['valueInt'] = np.select(conditions, values, default=df['value'])
df['valueInt'] = df['valueInt'].fillna(0)
df['valueInt'] = pd.to_numeric(df['valueInt'])
# Convert additional indicator valueInt column to discrete categorical variables for inflation color scheme
subCondition = ((df['indicator'] == 'Inflation (%)') | (df['indicator'] == 'Inflation (%)') | (
df['indicator'] == 'Food Inflation (%)'))
conditions = [
(df['valueInt'] > 30) & (df['valueInt'] < 100) & subCondition,
(df['valueInt'] >= 10) & (df['valueInt'] <= 30) & subCondition,
(df['valueInt'] > 0) & (df['valueInt'] < 10) & subCondition,
(df['valueInt'] > -100) & (df['valueInt'] < 0) & subCondition,
(df['value'] == "")
]
values = [1, 2, 3, 3, None]
df['valueInt'] = np.select(conditions, values, default=df['valueInt'])
# Convert additional indicator valueInt column to discrete categorical variables for currency color scheme
subCondition = ((df['indicator'] == 'Currency Exchange (YoY, %)'))
conditions = [
(df['valueInt'] > -100) & (df['valueInt'] < -25) & subCondition,
(df['valueInt'] >= -25) & (df['valueInt'] < -15) & subCondition,
(df['valueInt'] >= -15) & (df['valueInt'] < 0) & subCondition,
(df['valueInt'] > 0) & (df['valueInt'] < 100) & subCondition,
(df['value'] == "")
]
values = [1, 2, 3, 3, None]
df['valueInt'] = np.select(conditions, values, default=df['valueInt'])
# This is just to remove the extraneous indicators on the map
df.info()
df = df[~df['indicator'].isin(['Dependency on Russia or Ukraine',
'Cereal Import Dependency',
'Fertilizer Consumption',
'Debt Distress',
'GDP'])]
# Then add a column of icons for the mvam data
conditions = [
(mvam['status'] == 'Active'),
(mvam['status'] == 'Upcoming')
]
values = ["<span style='font-size:24px;color:#80bede'><i class='fas fa-wifi'></i>",
"<span style='font-size:24px;color:#bbbbbb'><i class='fas fa-expand'></i>"]
mvam['icon'] = np.select(conditions, values, default=mvam['status'])
# Create a smaller version of our geojson data
# View
mvam.info()
df.info()
summaries.info()
indicators.info()
# Export
# df.to_csv("test2.csv", index=False, encoding='utf8')
df.to_json(path_or_buf="data/main.json", orient='values')
indicators.to_json(path_or_buf="data/indicators.json", orient='values')
mvam.to_json(path_or_buf="data/mvam.json", orient='values')
summaries.to_json(path_or_buf="data/summaries.json", orient='values')