|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "# Bibliotecas \n", |
| 10 | + "\n", |
| 11 | + "import basedosdados as bd\n", |
| 12 | + "import pandas as pd\n", |
| 13 | + "import matplotlib.pyplot as plt\n", |
| 14 | + "%matplotlib inline" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 3, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [ |
| 22 | + { |
| 23 | + "name": "stderr", |
| 24 | + "output_type": "stream", |
| 25 | + "text": [ |
| 26 | + "Downloading: 100%|██████████| 153876/153876 [00:26<00:00, 5710.56rows/s]\n" |
| 27 | + ] |
| 28 | + } |
| 29 | + ], |
| 30 | + "source": [ |
| 31 | + "df_ocorrencias_full = bd.read_table(\n", |
| 32 | + " dataset_id='br_sp_gov_ssp',\n", |
| 33 | + " table_id='ocorrencias_registradas',\n", |
| 34 | + " billing_project_id='opendataday2024-416318'\n", |
| 35 | + ")" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 4, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "# Tratamento do campo de municipio\n", |
| 45 | + "df_ocorrencias_full['id_municipio'] = pd.to_numeric(df_ocorrencias_full['id_municipio'], errors='coerce').astype('Int64')" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 5, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "# Filtro Baixda Santista\n", |
| 55 | + "\n", |
| 56 | + "# Lista dos municipios da baixada santista usada para filtro\n", |
| 57 | + "municipios = [3548500,3551009,3541000,3518701,3513504,3506359]\n", |
| 58 | + "\n", |
| 59 | + "\n", |
| 60 | + "#Filtro dataset esgotameto\n", |
| 61 | + "df_ocorrencias_bx = df_ocorrencias_full[df_ocorrencias_full['id_municipio'].isin(municipios)]\n", |
| 62 | + "\n", |
| 63 | + "\n", |
| 64 | + "# Tratamento para adicionar a descrição da cidade\n", |
| 65 | + "descricao_dict = {3548500: 'Santos', 3551009: 'São Vicente', 3541000: 'Praia Grande', 3518701: 'Guarujá', 3513504: 'Cubatão', 3506359: 'Bertioga'}\n", |
| 66 | + "df_ocorrencias_bx['Descricao_Cidade'] = df_ocorrencias_bx['id_municipio'].map(descricao_dict)\n" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": 28, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [ |
| 74 | + { |
| 75 | + "data": { |
| 76 | + "text/plain": [ |
| 77 | + "ano Int64\n", |
| 78 | + "mes Int64\n", |
| 79 | + "id_municipio Int64\n", |
| 80 | + "regiao_ssp object\n", |
| 81 | + "homicidio_doloso Int64\n", |
| 82 | + "numero_de_vitimas_em_homicidio_doloso Int64\n", |
| 83 | + "homicidio_doloso_por_acidente_de_transito Int64\n", |
| 84 | + "numero_de_vitimas_em_homicidio_doloso_por_acidente_de_transito Int64\n", |
| 85 | + "homicidio_culposo_por_acidente_de_transito Int64\n", |
| 86 | + "homicidio_culposo_outros Int64\n", |
| 87 | + "tentativa_de_homicidio Int64\n", |
| 88 | + "lesao_corporal_seguida_de_morte Int64\n", |
| 89 | + "lesao_corporal_dolosa Int64\n", |
| 90 | + "lesao_corporal_culposa_por_acidente_de_transito Int64\n", |
| 91 | + "lesao_corporal_culposa_outras Int64\n", |
| 92 | + "latrocinio Int64\n", |
| 93 | + "numero_de_vitimas_em_latrocinio Int64\n", |
| 94 | + "total_de_estupro Int64\n", |
| 95 | + "estupro Int64\n", |
| 96 | + "estupro_de_vulneravel Int64\n", |
| 97 | + "total_de_roubo_outros Int64\n", |
| 98 | + "roubo_outros Int64\n", |
| 99 | + "roubo_de_veiculo Int64\n", |
| 100 | + "roubo_a_banco Int64\n", |
| 101 | + "roubo_de_carga Int64\n", |
| 102 | + "furto_outros Int64\n", |
| 103 | + "furto_de_veiculo Int64\n", |
| 104 | + "Descricao_Cidade object\n", |
| 105 | + "dtype: object" |
| 106 | + ] |
| 107 | + }, |
| 108 | + "execution_count": 28, |
| 109 | + "metadata": {}, |
| 110 | + "output_type": "execute_result" |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [ |
| 114 | + "df_ocorrencias_bx.dtypes" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "df_ocorrencias_bx.head(10)\n", |
| 124 | + "\n", |
| 125 | + "df_ocorrencias_bx_2021 = df_ocorrencias_bx[df_ocorrencias_bx['ano'] == 2021]\n", |
| 126 | + "df_ocorrencias_bx_2021.head(15)" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "code", |
| 131 | + "execution_count": 10, |
| 132 | + "metadata": {}, |
| 133 | + "outputs": [ |
| 134 | + { |
| 135 | + "data": { |
| 136 | + "text/html": [ |
| 137 | + "<div>\n", |
| 138 | + "<style scoped>\n", |
| 139 | + " .dataframe tbody tr th:only-of-type {\n", |
| 140 | + " vertical-align: middle;\n", |
| 141 | + " }\n", |
| 142 | + "\n", |
| 143 | + " .dataframe tbody tr th {\n", |
| 144 | + " vertical-align: top;\n", |
| 145 | + " }\n", |
| 146 | + "\n", |
| 147 | + " .dataframe thead th {\n", |
| 148 | + " text-align: right;\n", |
| 149 | + " }\n", |
| 150 | + "</style>\n", |
| 151 | + "<table border=\"1\" class=\"dataframe\">\n", |
| 152 | + " <thead>\n", |
| 153 | + " <tr style=\"text-align: right;\">\n", |
| 154 | + " <th></th>\n", |
| 155 | + " <th></th>\n", |
| 156 | + " <th>lesao_corporal_seguida_de_morte</th>\n", |
| 157 | + " <th>tentativa_de_homicidio</th>\n", |
| 158 | + " <th>homicidio_culposo_outros</th>\n", |
| 159 | + " <th>roubo_outros</th>\n", |
| 160 | + " </tr>\n", |
| 161 | + " <tr>\n", |
| 162 | + " <th>ano</th>\n", |
| 163 | + " <th>Descricao_Cidade</th>\n", |
| 164 | + " <th></th>\n", |
| 165 | + " <th></th>\n", |
| 166 | + " <th></th>\n", |
| 167 | + " <th></th>\n", |
| 168 | + " </tr>\n", |
| 169 | + " </thead>\n", |
| 170 | + " <tbody>\n", |
| 171 | + " <tr>\n", |
| 172 | + " <th rowspan=\"5\" valign=\"top\">2002</th>\n", |
| 173 | + " <th>Bertioga</th>\n", |
| 174 | + " <td>0</td>\n", |
| 175 | + " <td>16</td>\n", |
| 176 | + " <td>0</td>\n", |
| 177 | + " <td>0</td>\n", |
| 178 | + " </tr>\n", |
| 179 | + " <tr>\n", |
| 180 | + " <th>Cubatão</th>\n", |
| 181 | + " <td>0</td>\n", |
| 182 | + " <td>80</td>\n", |
| 183 | + " <td>1</td>\n", |
| 184 | + " <td>0</td>\n", |
| 185 | + " </tr>\n", |
| 186 | + " <tr>\n", |
| 187 | + " <th>Guarujá</th>\n", |
| 188 | + " <td>0</td>\n", |
| 189 | + " <td>139</td>\n", |
| 190 | + " <td>4</td>\n", |
| 191 | + " <td>0</td>\n", |
| 192 | + " </tr>\n", |
| 193 | + " <tr>\n", |
| 194 | + " <th>Praia Grande</th>\n", |
| 195 | + " <td>0</td>\n", |
| 196 | + " <td>112</td>\n", |
| 197 | + " <td>9</td>\n", |
| 198 | + " <td>0</td>\n", |
| 199 | + " </tr>\n", |
| 200 | + " <tr>\n", |
| 201 | + " <th>Santos</th>\n", |
| 202 | + " <td>0</td>\n", |
| 203 | + " <td>89</td>\n", |
| 204 | + " <td>0</td>\n", |
| 205 | + " <td>0</td>\n", |
| 206 | + " </tr>\n", |
| 207 | + " <tr>\n", |
| 208 | + " <th>...</th>\n", |
| 209 | + " <th>...</th>\n", |
| 210 | + " <td>...</td>\n", |
| 211 | + " <td>...</td>\n", |
| 212 | + " <td>...</td>\n", |
| 213 | + " <td>...</td>\n", |
| 214 | + " </tr>\n", |
| 215 | + " <tr>\n", |
| 216 | + " <th rowspan=\"5\" valign=\"top\">2021</th>\n", |
| 217 | + " <th>Cubatão</th>\n", |
| 218 | + " <td>0</td>\n", |
| 219 | + " <td>10</td>\n", |
| 220 | + " <td>0</td>\n", |
| 221 | + " <td>264</td>\n", |
| 222 | + " </tr>\n", |
| 223 | + " <tr>\n", |
| 224 | + " <th>Guarujá</th>\n", |
| 225 | + " <td>0</td>\n", |
| 226 | + " <td>24</td>\n", |
| 227 | + " <td>0</td>\n", |
| 228 | + " <td>1726</td>\n", |
| 229 | + " </tr>\n", |
| 230 | + " <tr>\n", |
| 231 | + " <th>Praia Grande</th>\n", |
| 232 | + " <td>1</td>\n", |
| 233 | + " <td>22</td>\n", |
| 234 | + " <td>2</td>\n", |
| 235 | + " <td>1543</td>\n", |
| 236 | + " </tr>\n", |
| 237 | + " <tr>\n", |
| 238 | + " <th>Santos</th>\n", |
| 239 | + " <td>1</td>\n", |
| 240 | + " <td>19</td>\n", |
| 241 | + " <td>0</td>\n", |
| 242 | + " <td>1140</td>\n", |
| 243 | + " </tr>\n", |
| 244 | + " <tr>\n", |
| 245 | + " <th>São Vicente</th>\n", |
| 246 | + " <td>0</td>\n", |
| 247 | + " <td>6</td>\n", |
| 248 | + " <td>1</td>\n", |
| 249 | + " <td>1180</td>\n", |
| 250 | + " </tr>\n", |
| 251 | + " </tbody>\n", |
| 252 | + "</table>\n", |
| 253 | + "<p>120 rows × 4 columns</p>\n", |
| 254 | + "</div>" |
| 255 | + ], |
| 256 | + "text/plain": [ |
| 257 | + " lesao_corporal_seguida_de_morte \\\n", |
| 258 | + "ano Descricao_Cidade \n", |
| 259 | + "2002 Bertioga 0 \n", |
| 260 | + " Cubatão 0 \n", |
| 261 | + " Guarujá 0 \n", |
| 262 | + " Praia Grande 0 \n", |
| 263 | + " Santos 0 \n", |
| 264 | + "... ... \n", |
| 265 | + "2021 Cubatão 0 \n", |
| 266 | + " Guarujá 0 \n", |
| 267 | + " Praia Grande 1 \n", |
| 268 | + " Santos 1 \n", |
| 269 | + " São Vicente 0 \n", |
| 270 | + "\n", |
| 271 | + " tentativa_de_homicidio homicidio_culposo_outros \\\n", |
| 272 | + "ano Descricao_Cidade \n", |
| 273 | + "2002 Bertioga 16 0 \n", |
| 274 | + " Cubatão 80 1 \n", |
| 275 | + " Guarujá 139 4 \n", |
| 276 | + " Praia Grande 112 9 \n", |
| 277 | + " Santos 89 0 \n", |
| 278 | + "... ... ... \n", |
| 279 | + "2021 Cubatão 10 0 \n", |
| 280 | + " Guarujá 24 0 \n", |
| 281 | + " Praia Grande 22 2 \n", |
| 282 | + " Santos 19 0 \n", |
| 283 | + " São Vicente 6 1 \n", |
| 284 | + "\n", |
| 285 | + " roubo_outros \n", |
| 286 | + "ano Descricao_Cidade \n", |
| 287 | + "2002 Bertioga 0 \n", |
| 288 | + " Cubatão 0 \n", |
| 289 | + " Guarujá 0 \n", |
| 290 | + " Praia Grande 0 \n", |
| 291 | + " Santos 0 \n", |
| 292 | + "... ... \n", |
| 293 | + "2021 Cubatão 264 \n", |
| 294 | + " Guarujá 1726 \n", |
| 295 | + " Praia Grande 1543 \n", |
| 296 | + " Santos 1140 \n", |
| 297 | + " São Vicente 1180 \n", |
| 298 | + "\n", |
| 299 | + "[120 rows x 4 columns]" |
| 300 | + ] |
| 301 | + }, |
| 302 | + "execution_count": 10, |
| 303 | + "metadata": {}, |
| 304 | + "output_type": "execute_result" |
| 305 | + } |
| 306 | + ], |
| 307 | + "source": [ |
| 308 | + "df_ocorrencias_bx.groupby(['ano', 'Descricao_Cidade'])['lesao_corporal_seguida_de_morte','tentativa_de_homicidio','homicidio_culposo_outros','roubo_outros'].sum()" |
| 309 | + ] |
| 310 | + }, |
| 311 | + { |
| 312 | + "cell_type": "code", |
| 313 | + "execution_count": 7, |
| 314 | + "metadata": {}, |
| 315 | + "outputs": [ |
| 316 | + { |
| 317 | + "data": { |
| 318 | + "text/plain": [ |
| 319 | + "ano Descricao_Cidade\n", |
| 320 | + "2002 Bertioga 16\n", |
| 321 | + " Cubatão 80\n", |
| 322 | + " Guarujá 139\n", |
| 323 | + " Praia Grande 112\n", |
| 324 | + " Santos 89\n", |
| 325 | + " ... \n", |
| 326 | + "2021 Cubatão 10\n", |
| 327 | + " Guarujá 24\n", |
| 328 | + " Praia Grande 22\n", |
| 329 | + " Santos 19\n", |
| 330 | + " São Vicente 6\n", |
| 331 | + "Name: tentativa_de_homicidio, Length: 120, dtype: Int64" |
| 332 | + ] |
| 333 | + }, |
| 334 | + "execution_count": 7, |
| 335 | + "metadata": {}, |
| 336 | + "output_type": "execute_result" |
| 337 | + } |
| 338 | + ], |
| 339 | + "source": [ |
| 340 | + "df_ocorrencias_bx.groupby(['ano', 'Descricao_Cidade'])['tentativa_de_homicidio'].sum()" |
| 341 | + ] |
| 342 | + }, |
| 343 | + { |
| 344 | + "cell_type": "code", |
| 345 | + "execution_count": 9, |
| 346 | + "metadata": {}, |
| 347 | + "outputs": [ |
| 348 | + { |
| 349 | + "data": { |
| 350 | + "text/plain": [ |
| 351 | + "ano Descricao_Cidade\n", |
| 352 | + "2002 Bertioga 1\n", |
| 353 | + " Cubatão 5\n", |
| 354 | + " Guarujá 6\n", |
| 355 | + " Praia Grande 2\n", |
| 356 | + " Santos 16\n", |
| 357 | + " ..\n", |
| 358 | + "2021 Cubatão 20\n", |
| 359 | + " Guarujá 20\n", |
| 360 | + " Praia Grande 47\n", |
| 361 | + " Santos 10\n", |
| 362 | + " São Vicente 10\n", |
| 363 | + "Name: roubo_de_carga, Length: 120, dtype: Int64" |
| 364 | + ] |
| 365 | + }, |
| 366 | + "execution_count": 9, |
| 367 | + "metadata": {}, |
| 368 | + "output_type": "execute_result" |
| 369 | + } |
| 370 | + ], |
| 371 | + "source": [ |
| 372 | + "df_ocorrencias_bx.groupby(['ano', 'Descricao_Cidade'])['roubo_de_carga'].sum()" |
| 373 | + ] |
| 374 | + } |
| 375 | + ], |
| 376 | + "metadata": { |
| 377 | + "kernelspec": { |
| 378 | + "display_name": "Python 3", |
| 379 | + "language": "python", |
| 380 | + "name": "python3" |
| 381 | + }, |
| 382 | + "language_info": { |
| 383 | + "codemirror_mode": { |
| 384 | + "name": "ipython", |
| 385 | + "version": 3 |
| 386 | + }, |
| 387 | + "file_extension": ".py", |
| 388 | + "mimetype": "text/x-python", |
| 389 | + "name": "python", |
| 390 | + "nbconvert_exporter": "python", |
| 391 | + "pygments_lexer": "ipython3", |
| 392 | + "version": "3.8.10" |
| 393 | + } |
| 394 | + }, |
| 395 | + "nbformat": 4, |
| 396 | + "nbformat_minor": 2 |
| 397 | +} |
0 commit comments