-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathemdat_df.py
282 lines (220 loc) · 11.2 KB
/
emdat_df.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import pandas as pd
import wbdata
from utils import rebase_CPI
class emdat():
'''A class used to load, clean and manipulate EMDAT data.
Args:
filename (str) : the path to the Excel file to load.
Attributes:
data (pd.DataFrame) : returns the EMDAT data in df format.
countries (list) : returns the unique set of countries contained in the data.
hazard_types (list) : returns the unique set of hazard types contained in the data.
n_events (int) : returns the number of individual events contained in the data.
'''
def __init__(self,filename):
cols_dict = {"Year":"year",
"Country":"country",
"Dis No":"dis_no",
"Total Deaths":"deaths",
"Disaster Group":"disaster_group",
"Disaster Type":"disaster_type",
"Disaster Subgroup":"disaster_subgroup",
"Disaster Subtype":"disaster_subtype",
"Location":"location",
"Latitude":"lat",
"Longitude":"lon",
"Event Name":"event_name",
"No Injured":"injured",
"No Affected":"affected",
"No Homeless":"homeless",
"Total Affected":"total_affected",
"Reconstruction Costs ('000 US$)":"reconstruction_costs",
"Insured Damages ('000 US$)":"insured_damages",
"Total Damages ('000 US$)":"total_damages"}
self.data = pd.read_excel(filename, header = 6).rename(columns = cols_dict)
self.countries = self.data.country.unique()
self.disaster_types = self.data.disaster_type.unique()
self.n_events = self.data.dis_no.nunique()
def disaster_count_timeseries(self, min_year, max_year, countries, disastertype):
'''
Summarize the number of disasters for selected period, countries and disaster type.
Args:
min_year (int) : start year
max_year (int) : end year
countries (list) : countries using EMDAT's unique country names
disastertype (list) : disaster type using EMDAT's disaster type categorization
Returns
pd.Series : annual time series of disaster count (eg. yearly number of flood & storm events in Bulgaria and Romania, 1960-2000).
'''
df = self.data
if type(countries) == str: countries = [countries]
if type(disastertype) == str: disastertype = [disastertype]
if min_year and max_year:
df = df[(df.year > min_year) & (df.year < max_year)]
if countries == ['all'] or not countries:
pass
else:
df = df[df.index.isin(countries)]
if disastertype == ['all'] or not disastertype:
pass
else:
df = df[df['disaster_type'].isin(disastertype)]
result = df.groupby(['year','disaster_type'])['dis_no'].count()
result = result.unstack().fillna(0)
return(result)
def disaster_stats_entire_period(self, min_year, max_year, countries, disastertype, stats):
'''
EMDAT summary grouped by disaster type: breakdown for entire period.
Args:
min_year (int) : start year
max_year (int) : end year
countries (list) : countries using EMDAT's unique country names
disastertype (list) : disaster type using EMDAT's disaster type categorization
stats (list) : statistic to use (``deaths, injured, affected, total_affected, homeless, reconstruction_costs, insured_damages, total_damages``)
Returns
pd.Series : breakdown of selected disaster statistic for selected period (eg. people affected by flood, storm and earthquake in Pakistan for overall period 2000-2010).
'''
df = self.data
if type(countries) == str: countries = [countries]
if type(disastertype) == str: disastertype = [disastertype]
if min_year and max_year:
df = df[(df.year > str(min_year)) & (df.year < str(max_year))]
if countries == ['all'] or not countries:
pass
else:
df = df[df.country.isin(countries)]
if disastertype == ['all'] or not disastertype:
pass
else:
df = df[df['disaster_type'].isin(disastertype)]
result = df.groupby(['disaster_type'])[stats].sum()[stats]
result = result.fillna(0)[stats]
result = result.sort_values(by = stats, ascending = False)
return(result)
def disaster_stats_timeseries(self, min_year, max_year, countries, disastertype, stats):
'''
EMDAT summary grouped by disaster type: annual time series.
Args:
min_year (int) : start year
max_year (int) : end year
countries (list) : countries using EMDAT's unique country names
disastertype (list) : disaster type using EMDAT's disaster type categorization
stats (list) : statistic to use (``deaths, injured, affected, total_affected, homeless, reconstruction_costs, insured_damages, total_damages``)
Returns
pd.Series : annual time series of disaster count (eg. USD damages from flood, storm and earthquake in Latin American countries each year from 1960-2000.)
'''
df = self.data
if type(countries) == str: countries = [countries]
if type(disastertype) == str: disastertype = [disastertype]
if min_year and max_year:
df = df[(df.year > min_year) & (df.year < max_year)]
if countries == ['all'] or not countries:
pass
else:
df = df[df.country.isin(countries)]
if disastertype == ['all'] or not disastertype:
pass
else:
df = df[df['disaster_type'].isin(disastertype)]
result = df.groupby(['year','disaster_type'])[stats].sum()
result = result.unstack().fillna(0)
return(result)
def country_stats_entire_period(self, min_year, max_year, countries, disastertype, stats):
'''
EMDAT summary grouped by country: breakdown for entire period.
Args:
min_year (int) : start year
max_year (int) : end year
countries (list) : countries using EMDAT's unique country names
disastertype (list) : disaster type using EMDAT's disaster type categorization
stats (list) : statistic to use (``deaths, injured, affected, total_affected, homeless, reconstruction_costs, insured_damages, total_damages``)
Returns
pd.DataFrame : country breakdown of selected disaster statistics, total for period (eg. Nicaragua, Honduras and Belize USD flood damage, 2000-2020). '''
df = self.dat
if type(countries) == str: countries = [countries]
if type(disastertype) == str: disastertype = [disastertype]
if min_year and max_year:
df = df[(df.year > min_year) & (df.year < max_year)]
if countries == ['all'] or not countries:
pass
else:
df = df[df.country.isin(countries)]
if disastertype == ['all'] or not disastertype:
pass
else:
df = df[df['disaster_type'].isin(disastertype)]
result = df.groupby(['country'])[stats].sum()[stats]
result = result.unstack().fillna(0)
return(result)
def country_stats_timeseries(self, min_year, max_year, countries, disastertype, stats):
'''
EMDAT summary grouped by country: annual time series.
Args:
min_year (int) : start year
max_year (int) : end year
countries (list) : countries using EMDAT's unique country names
disastertype (list) : disaster type using EMDAT's disaster type categorization
stats (list) : statistic to use (``deaths, injured, affected, total_affected, homeless, reconstruction_costs, insured_damages, total_damages``)
Returns
pd.DataFrame : annual time series of disaster statistics grouped by country (eg. Pakistan and India flood, storm and earthquake USD damages each year, 1980-2020)
'''
df = self.data
if type(countries) == str: countries = [countries]
if type(disastertype) == str: disastertype = [disastertype]
if min_year and max_year:
df = df[(df.year > min_year) & (df.year < max_year)]
if countries == ['all'] or not countries:
pass
else:
df = df[df.country.isin(countries)]
if disastertype == ['all'] or not disastertype:
pass
else:
df = df[df['disaster_type'].isin(disastertype)]
result = df.groupby(['year','country'])[stats].sum()
result = result.unstack().fillna(0)
return(result)
def disaster_stats_total_for_period(self, min_year, max_year, countries, disastertype, stats):
'''
Single total for a given statistic.
Args:
min_year (int) : start year
max_year (int) : end year
countries (list) : countries using EMDAT's unique country names
disastertype (list) : disaster type using EMDAT's disaster type categorization
stats (list) : statistic to use (``deaths, injured, affected, total_affected, homeless, reconstruction_costs, insured_damages, total_damages``)
Returns
float : total for selected disaster statistic (eg. total people affected by earthquake in Pakistan, 1960-2020).
'''
df = self.data
if type(countries) == str: countries = [countries]
if type(disastertype) == str: disastertype = [disastertype]
if min_year and max_year:
df = df[(df.year > min_year) & (df.year < max_year)]
if countries == ['all'] or not countries:
pass
else:
df = df[df.country.isin(countries)]
if disastertype == ['all'] or not disastertype:
pass
else:
df = df[df['disaster_type'].isin(disastertype)]
return(df[stats].sum()[0])
def aal_by_disaster_type(self, min_year, max_year, countries, disastertype, base_year = 2010,damage_type = 'total_damages'):
# aal is total loss divided by number of years (but ignore years with no loss)
damage_by_hazard_df=self.disaster_stats_timeseries(min_year, max_year, countries, disastertype,stats=damage_type)
damage_by_hazard_real = damage_by_hazard_df.apply(rebase_CPI, axis=0, args=[base_year])
aal_series = damage_by_hazard_real.sum() / (damage_by_hazard_real.index.max() - damage_by_hazard_real.index.min())
return(aal_series.astype(int).sort_values(ascending=False))
def filter_years(self, min_year, max_year):
'''Filter data by start and end year'''
df = self.data
return df[(df.year > str(min_year)) & (df.year < str(max_year))]
def filter_countries(self, countries):
'''Filter data by country'''
df = self.data
return df[df.index.isin(countries)]
def filter_disastertype(self, disastertype):
'''Filter data by disaster type'''
df = self.data
return df[df['disaster_type'].isin(disastertype)]