-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
289 lines (221 loc) · 12.2 KB
/
convert.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
280
281
282
283
284
285
286
287
288
289
import pandas as pd
df = pd.read_excel('Base_Dados_Final.xlsx', sheet_name='BD')
list(df)
# Define a dictionary with the old column names as keys and new column names as values
translation_dict = {'Período': 'Period',
'Estado_Costeiro': 'Coastal_State',
'Tipo_Navio': 'Ship_Type',
'Bandeira': 'Flag',
'Estado do Navio': 'Ship_State',
'Proximidade_Costa(milhas)': 'Distance_from_Coast',
'ÁREA_NAVEGAÇÃO': 'Navigation_Area',
'Classificação_Ataque': 'Attack_Classification',
'Numero_Criminosos': 'N_of_Criminals',
'N_Criminosos': 'N_of_Criminals_cat',
'Armamento': 'Weaponry',
'Hijack': 'Hijack',
'Sequestro': 'Kidnapping',
'Rapto': 'Abduction',
'Feridos': 'Injured',
'Mortos': 'Dead',
'Nível_Proteção': 'Protection_Level',
'Resumo': 'Summary',
'Data_hora': 'Date_Time',
'vento_tam': 'Wind_Speed',
'vento_nova': 'Wind_Speed_cat',
'vento': 'Wind_Speed_cat_2',
'onda_nova': 'Wave_Size_cat',
'onda': 'Wave_Size_cat_2',
'onda_tam': 'Wave_Size',
'chuva': 'Rain_cat',
'chuva (mm/dia)': 'Rain',
'DATA': 'Date',
'Ano': 'Year',
'Mês': 'Month',
'Estação_Africa': 'African_Season',
'Estação': 'Season',
'Zona_Costeira': 'Coastal_Zone',
'Meteorologia': 'Meteorology',
'Meteorologia_nova': 'New_Meteorology',
'Nível_Ataque': 'Attack_Level',
'Risco_Bandeira': 'Flag_Risk',
'Sucesso_Ataque': 'Attack_Success',
'lat_d': 'Lat_D',
'lon_d': 'Lon_D',
'Ajuda_Autoridades': 'Assistance_from_Authorities',
'Perigosidade': 'Dangerousness'}
# Rename the columns using the dictionary
df = df.rename(columns=translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Noturno': 'Night',
'Diurno': 'Day'}
# Use the replace method to translate the values in the Period column
df['Period'] = df['Period'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Nigéria': 'Nigeria',
'Guiné': 'Guinea',
'Camarões': 'Cameroon',
'Benim': 'Benin',
'Gana': 'Ghana',
'República Dem. Congo': 'Democratic Republic of the Congo',
'Togo': 'Togo',
'Serra Leoa': 'Sierra Leone',
'Congo': 'Congo',
'Angola': 'Angola',
'Costa do Marfim': 'Ivory Coast',
'Gabão': 'Gabon',
'S.Tomé e Príncipe': 'Sao Tome and Principe',
'Libéria': 'Liberia',
'Guiné Equatorial': 'Equatorial Guinea',
'Guiné-Equatorial': 'Equatorial Guinea'}
# Use the replace method to translate the values in the Coastal_State column
df['Coastal_State'] = df['Coastal_State'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Outros': 'Other'}
# Use the replace method to translate the values in the Ship_Type column
df['Ship_Type'] = df['Ship_Type'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Outros': 'Other',
'IlhasMarshall': 'Marshall Islands',
'Nigéria': 'Nigeria',
'Malta': 'Malta',
'China': 'China',
'Libéria': 'Liberia',
'Panamá': 'Panama',
'Singapura': 'Singapore'}
# Use the replace method to translate the values in the Flag column
df['Flag'] = df['Flag'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Navegar': 'Sailing',
'Fundeado': 'Anchored',
'Atracado': 'Docked'}
# Use the replace method to translate the values in the Ship_State column
df['Ship_State'] = df['Ship_State'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Águas Internacionais': 'International Waters',
'Mar Territorial': 'Territorial Waters',
'Área Portuária': 'Port Area'}
# Use the replace method to translate the values in the Navigation_Area column
df['Navigation_Area'] = df['Navigation_Area'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'NCONSEGUIDO': 'Unsuccessful',
'ROUBO': 'Theft',
'SEQUESTRO': 'Kidnapping',
'HIJACK': 'Hijacking',
'RAPTO': 'Abduction'}
# Use the replace method to translate the values in the Attack_Classification column
df['Attack_Classification'] = df['Attack_Classification'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Médio': 'Medium',
'Baixo': 'Low',
'Alto': 'High'}
# Use the replace method to translate the values in the Protection_Level column
df['Protection_Level'] = df['Protection_Level'].replace(translation_dict)
# Convert the Date_Time column to a pandas Datetime data type
df['Date_Time'] = pd.to_datetime(df['Date_Time'])
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Fraco': 'Gentle_breeze',
'Moderado': 'Moderate_breeze',
'Aragem': 'Light_breeze',
'Bafagem': 'Light_air',
'Fresco': 'Fresh_breeze'}
# Use the replace method to translate the values in the Wind_Speed_cat column
df['Wind_Speed_cat'] = df['Wind_Speed_cat'].replace(translation_dict)
df['Wind_Speed_cat_2'] = df['Wind_Speed_cat_2'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Pequena Vaga': 'Slight',
'Cavado': 'Moderate',
'Encrespado': 'Smooth'}
# Use the replace method to translate the values in the Wave_Size_cat column
df['Wave_Size_cat'] = df['Wave_Size_cat'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Crescente': 'Increasing',
'Mediana': 'Moderate',
'Elevada': 'High'}
# Use the replace method to translate the values in the Wave_Size_cat_2 column
df['Wave_Size_cat_2'] = df['Wave_Size_cat_2'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Frio e Húmido': 'Cold and Humid',
'Seco e Quente': 'Dry and Hot'}
# Use the replace method to translate the values in the Africa_Season column
df['African_Season'] = df['African_Season'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Primavera': 'Spring',
'Verão': 'Summer',
'Outono': 'Fall/Autumn',
'Inverno': 'Winter'}
# Use the replace method to translate the values in the Season column
df['Season'] = df['Season'].replace(translation_dict)
# Define a dictionary with the numerical codes as keys and English zone names as values
translation_dict = {'Zona 1': 'Zone 1',
'Zona 2': 'Zone 2',
'Zona 3': 'Zone 3',
'Zona 4': 'Zone 4'}
# Use the replace method to translate the values in the Coastal_Zone column
df['Coastal_Zone'] = df['Coastal_Zone'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Baixo': 'Low',
'Médio': 'Medium',
'Alto': 'High'}
# Use the replace method to translate the values in the Flag_Risk column
df['Flag_Risk'] = df['Flag_Risk'].replace(translation_dict)
# Define a dictionary with the Portuguese values as keys and English values as values
translation_dict = {'Facas':'Knives',
'Arma de Fogo': 'Firearms',
'Desconhecido': 'Unknown'}
# Use the replace method to translate the values in the Flag_Risk column
df['Weaponry'] = df['Weaponry'].replace(translation_dict)
# Drop some variables that are not needed
df.drop(columns=['Wind_Speed_cat_2','Wave_Size_cat_2','New_Meteorology'], inplace=True)
data_final = df[['Period','Date_Time','Date','Year',
'Month','Season','African_Season',
'Lat_D','Lon_D','Coastal_State',
'Coastal_Zone','Distance_from_Coast',
'Navigation_Area','Wind_Speed',
'Wind_Speed_cat','Wave_Size',
'Wave_Size_cat','Rain','Rain_cat',
'Meteorology','Ship_Type','Flag',
'Ship_State','Flag_Risk','Protection_Level',
'N_of_Criminals','N_of_Criminals_cat',
'Weaponry','Attack_Classification',
'Hijack','Kidnapping','Abduction',
'Attack_Level','Dangerousness','Summary',
'Injured','Dead','Attack_Success',
'Assistance_from_Authorities']]
data_final.to_excel('piracy_guinea_gulf.xlsx')
data_final.to_csv('piracy_guinea_gulf.csv')
data_final.to_pickle('piracy_guinea_gulf.pickle')
''''''''''''''''''''''''''''''''''''''''''''
import folium
from folium.plugins import MarkerCluster
from folium.vector_layers import Rectangle
# Define a function to save the map view as an image
def save_map_view(map):
map.save('map_view.png')
# Get the center latitude and longitude values of the dataframe
center_lat = (df.Lat_D.max() + df.Lat_D.min()) / 2
center_lon = (df.Lon_D.max() + df.Lon_D.min()) / 2
# Calculate the dimensions of the rectangle based on the minimum and maximum latitude and longitude values in the dataframe
width = df.Lon_D.max() - df.Lon_D.min()
height = df.Lat_D.max() - df.Lat_D.min()
# Create a map object centered on the center latitude and longitude values, with an appropriate zoom level
m = folium.Map(location=[center_lat, center_lon], zoom_start=6)
# Create a marker cluster object to group markers together for better performance
marker_cluster = MarkerCluster().add_to(m)
# Loop over each row in the dataframe and add a marker to the map for each location
for index, row in df.iterrows():
folium.Marker(location=[row['Lat_D'], row['Lon_D']],
popup=row[['Date_Time','Attack_Classification','Weaponry']]).add_to(marker_cluster)
# Create a rectangle object with the dimensions and location calculated earlier
rectangle = Rectangle(bounds=[[df.Lat_D.min(), df.Lon_D.min()], [df.Lat_D.max(), df.Lon_D.max()]],
color='red', fill=False).add_to(m)
# Add a custom control to the map to allow the user to save the map view as an image
folium.plugins.Fullscreen().add_to(m)
# Display the map
m.save('piracy.html')
for column in df.columns:
print(f"Column: {column}")
print(df[column].value_counts())
for column in df.columns:
print(f"Column: {column}")
print(df[column].describe())