-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal-web-scrap-medium.html
419 lines (356 loc) · 105 KB
/
final-web-scrap-medium.html
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html>
<html><head>
<title>Final web scrap medium</title>
<base href="./">
<meta id="root-path" root-path="./">
<link rel="icon" sizes="96x96" href="https://publish-01.obsidian.md/access/f786db9fac45774fa4f0d8112e232d67/favicon-96x96.png">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes, minimum-scale=1.0, maximum-scale=5.0">
<meta charset="UTF-8">
<script src="https://code.iconify.design/iconify-icon/1.0.3/iconify-icon.min.js"></script>
<link rel="stylesheet" href="lib/styles/obsidian-styles.css">
<link rel="stylesheet" href="lib/styles/theme.css">
<link rel="stylesheet" href="lib/styles/plugin-styles.css">
<link rel="stylesheet" href="lib/styles/snippets.css">
<style> </style>
<!-- Graph View Data -->
<script>
let nodes=
{"nodeCount":27,"linkCount":20,"radii":[7,7,7,7,7,6.75,6,6,6,4.75,4.75,4.75,4.75,4.75,4.75,3,3,3,3,3,3,3,3,3,3,3,3],"labels":["Syllabus","Day 5","Day 2","Day 1","Datasets","Day 3","National Survey of Child Health","Example 1","Add Health","Final web scrap medium","Consumer Complaints","Fatal police shootings","Housing and Transportation Affordability Index","Household Pulse Survey Data Tables","Day 4","Highlighting structure","Cheat Sheet","Lit Reviews","Research Reminders","Note taking","Daily Note","Example Template","Research Notes","focus_group","Research Paper","Research Ideas","research notes"],"paths":["syllabus.html","day-5.html","day-2.html","day-1.html","datasets.html","day-3.html","national-survey-of-child-health.html","example-1.html","add-health.html","final-web-scrap-medium.html","consumer-complaints.html","fatal-police-shootings.html","housing-and-transportation-affordability-index.html","household-pulse-survey-data-tables.html","day-4.html","00-meta/001-structure/highlighting-structure.html","00-meta/001-structure/cheat-sheet.html","00-meta/003-tools/lit-reviews.html","00-meta/003-tools/research-reminders.html","00-meta/003-tools/note-taking.html","00-meta/002-templates/daily-note.html","00-meta/002-templates/example-template.html","00-meta/002-templates/research-notes.html","00-meta/002-templates/meta/zdc-templates/focus_group.html","00-meta/002-templates/meta/zdc-templates/research-paper.html","00-meta/002-templates/meta/zdc-templates/research-ideas.html","00-meta/002-templates/meta/zdc-templates/research-notes.html"],"linkSources":[4,4,4,4,4,3,3,3,2,2,5,1,1,1,1,0,0,0,0,0],"linkTargets":[8,10,12,6,13,9,7,4,7,6,11,8,3,2,5,3,2,5,14,1]};
let attractionForce = 1;
let linkLength = 10;
let repulsionForce = 150;
let centralForce = 3;
let edgePruning = 100;
</script>
<script type="module" src="lib/scripts/graph_view.js"></script>
<script src="lib/scripts/graph_wasm.js"></script>
<script src="lib/scripts/tinycolor.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/7.2.4/pixi.min.js" integrity="sha512-Ch/O6kL8BqUwAfCF7Ie5SX1Hin+BJgYH4pNjRqXdTEqMsis1TUYg+j6nnI9uduPjGaj7DN4UKCZgpvoExt6dkw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="lib/scripts/webpage.js"></script>
</head><body class="theme-dark mod-windows is-frameless is-hidden-frameless obsidian-app show-inline-title show-view-header outliner-plugin-better-lists outliner-plugin-dnd" style="--zoom-factor:1.2; --font-text-size:16px; --line-width:50em; --line-width-adaptive:50em; --file-line-width:50em; --content-width:500em; --sidebar-width:25em; --collapse-arrow-size:0.4em; --tree-horizontal-spacing:1em; --tree-vertical-spacing:0.5em; --sidebar-margin:12px;"><div class="webpage-container"><div class="sidebar-left sidebar"><div class="sidebar-content"><div><label class="theme-toggle-container" for="theme_toggle"><input class="theme-toggle-input" type="checkbox" id="theme_toggle"><div class="toggle-background"></div></label></div><div class="tree-container file-tree mod-nav-indicator" data-depth="0"><div class="tree-header"><span class="sidebar-section-header">Bootcamp</span><button class="clickable-icon collapse-tree-button is-collapsed"><iconify-icon icon="ph:arrows-in-line-horizontal-bold" width="18px" height="18px" rotate="90deg" color="currentColor"></iconify-icon></button></div><div class="tree-scroll-area"></div></div></div></div><div class="document-container"><div class="markdown-preview-view markdown-rendered node-insert-event allow-fold-headings show-indentation-guide allow-fold-lists" tabindex="-1" style="tab-size: 4;"><style id="MJX-CHTML-styles"></style><div class="markdown-preview-sizer markdown-preview-section" style="padding-bottom: ; padding-top: var(--file-margins); padding-right: var(--file-margins); padding-left: var(--file-margins); width: 100%; position: absolute;"><div class="markdown-preview-pusher" style="width: 1px; height: 0.1px; margin-bottom: 0px;"></div><div class="mod-header"><div class="inline-title" contenteditable="true" spellcheck="true" autocapitalize="on" tabindex="-1" enterkeyhint="done">Final web scrap medium</div></div><div><h1 data-heading="Mining Homicides in Los Angeles County" id="Mining_Homicides_in_Los_Angeles_County">Mining Homicides in Los Angeles County</h1></div><div><h2 data-heading="Background" id="Background" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Background</h2></div><div><p>The new documentary called "The Trials of Gabriel Fernandez" brings to the forefront the horrendous abuse that children in Los Angeles County and elsewhere endure at the hands of their caregivers. The documentary is a six-part miniseries about the 2013 murder of Gabriel Fernandez, an 8-year old Palmdale boy, that aired on Netflix February, 2020. In 2013, Gabriel Fernandez died in the neighborhood of LA county called Palmdale. For years, Gabriel was abused by his mother and her boyfriend. The abuse included being beaten, shot with a BB gun, smothered with pepper spray, made to wear girl’s clothes to school, forced to eat his own vomit, and locked in a cabinet with a sock stuffed in his mouth. His biological father was incarcerated previously. From 2003 – 2012 over 60 complaints were filed leading to 8 investigations by Child Protective Services. His mother and her boyfriend were given life sentences for his death. The social workers were charged with felony child abuse and falsifying public documents. </p></div><div><p>The case of Gabriel Fernandez demonstrates the complexity of the risk factors and the profound psychological impact that fatal child maltreatment has on members of the community. It also highlights aspects of our social, legal and criminal justice systems that are in desperate need of reform. Gabriel’s teacher, family and even the prosecutor discussed the psychological toll it took on them. Apparently, their pleas for help from Child Protective Services were of no avail. </p></div><div><p>What first piqued my interest about this case is that it happened again, about 5 years later, when 4-year old Anthony Alvalos was murdered 15 minutes from Garbiel’s home. The cases were almost identical, even regarding the fact that Anthony’s parents thought he, too, was gay.</p></div><div><h2 data-heading="My Work" id="My_Work" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>My Work</h2></div><div><p>As a researcher of child abuse and neglect, I have known about the Gabriel Fernandez case for years. My own work explores the structural aspects of child welfare across time and space. If I could boil this work down to one central message it would be that neighborhood characteristics, in addition to the characteristics of children and families, are critical components of child maltreatment prevention. Nevertheless, the characteristics of neighborhoods are often overlooked in preventive interventions. The characteristics of neighborhoods are important to understand for many reasons. One of the most important reasons is because they reflect the structural components of race and class and how they play themselves out in the area of child abuse. </p></div><div><h2 data-heading="The Problem" id="The_Problem" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>The Problem</h2></div><div><p>One difficulty that I have encountered in my research is the lack of data on child abuse and neglect, particularly that which pertains to child homicides. This has forced me to think outside the box about data and data collection efforts. As a methodologist, as well, I understand full well that innovative methods of machine learning are going to be an important component of applied research. Whereas it is relatively easy to give answers to well-structured questions, when there is no structure to the data standard statistical analyses are inapplicable. Unstructured data makes up 80-90% of all data produced by organizations (Holzinger, Stocker et al., 2013) including police reports, court documents and human service organizations. In order to understand patterns in such data, machine learning algorithms such as text mining and topic modeling can help given meaning to narrative, unstructured text. Therefore, understanding the power of data science, including web scraping and text mining, is a critical part of developing a holistic approach to child protection. In this first article, I will illustrate how I created a dataset of all child homicides in Los Angeles County by scraping the Los Angeles Times Homicide report located <a data-tooltip-position="top" aria-label="https://homicide.latimes.com/" rel="noopener" class="external-link" href="https://homicide.latimes.com/" target="_blank">here</a></p></div><div><p>The first step (see figure 1) in this project was to collect geospatial data on child homicides. </p></div><div><h2 data-heading="Preliminaries" id="Preliminaries" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Preliminaries</h2></div><div><p>I am using Python to perform the web scraping, data cleaning and mapping. Before you begin, make sure you have the latest version of Python installed on your computer. I used homebrew for the installation and it is straghtforward if you follow the steps listed here. Next, in order to scrape the website, you must make sure to install the following packages. To do so, you can type the following in the terminal window on a Mac or Unix machine: </p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">pip3 install <span class="token punctuation">[</span>package<span class="token punctuation">.</span>name<span class="token punctuation">]</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><p>Below are the packages I used for the web scraping. </p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded"><span class="token comment">#pd.options.mode.chained_assignment = None # see https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas</span>
<span class="token keyword">import</span> urllib
<span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>support<span class="token punctuation">.</span>ui <span class="token keyword">import</span> WebDriverWait
<span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> urllib
<span class="token keyword">from</span> urllib<span class="token punctuation">.</span>parse <span class="token keyword">import</span> urljoin
<span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>chrome<span class="token punctuation">.</span>service <span class="token keyword">import</span> Service
<span class="token keyword">from</span> webdriver_manager<span class="token punctuation">.</span>chrome <span class="token keyword">import</span> ChromeDriverManager
<span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>support<span class="token punctuation">.</span>ui <span class="token keyword">import</span> WebDriverWait
<span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>support <span class="token keyword">import</span> expected_conditions <span class="token keyword">as</span> EC
<span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>common<span class="token punctuation">.</span>by <span class="token keyword">import</span> By
<span class="token keyword">from</span> selenium <span class="token keyword">import</span> webdriver
<span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>common<span class="token punctuation">.</span>keys <span class="token keyword">import</span> Keys
<span class="token keyword">from</span> selenium<span class="token punctuation">.</span>webdriver<span class="token punctuation">.</span>chrome<span class="token punctuation">.</span>options <span class="token keyword">import</span> Options
<span class="token keyword">import</span> time
<span class="token keyword">from</span> bs4 <span class="token keyword">import</span> BeautifulSoup <span class="token keyword">as</span> bs
<span class="token keyword">import</span> requests
<span class="token keyword">import</span> re
<span class="token keyword">import</span> pandas <span class="token keyword">as</span> pd
<span class="token keyword">from</span> pandas <span class="token keyword">import</span> ExcelWriter
</code><button class="copy-code-button">Copy</button></pre></div><div><h2 data-heading="Data Extraction" id="Data_Extraction" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Data Extraction</h2></div><div><p>To understand the process, first go to the LA Times website and right under the map there is a search feature. Click the 'More' button to perform an advanced search. Click the down arrow next to the box that says 'Last 12 months' and select 'All years' (which is the last option). Then click the down arrow next to the box that says 'Age' and select 0-5. This search will result in all homicides to children under the age of 6 years old spanning 2000 - 2020. Some of the results are shown in figure 3.</p></div><div><p>If you go to the L.A. Times website, you will notice that the page loads dynamically. Therefore, flow is to wait for the webpage to load, grab the data, then simulate scrolling down the page in order to grab the next few homicides, and repeat until all of the homicide cases are downloaded.</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">chrome_options <span class="token operator">=</span> Options<span class="token punctuation">(</span><span class="token punctuation">)</span>
chrome_options<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">'disable_infobars'</span><span class="token punctuation">)</span>
<span class="token comment"># Start the WebDriver and load the page</span>
wd <span class="token operator">=</span> webdriver<span class="token punctuation">.</span>Chrome<span class="token punctuation">(</span>ChromeDriverManager<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>install<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> options<span class="token operator">=</span>chrome_options<span class="token punctuation">)</span>
<span class="token comment">#wd = webdriver.Chrome(options=chrome_options)</span>
wd<span class="token punctuation">.</span>delete_all_cookies<span class="token punctuation">(</span><span class="token punctuation">)</span>
wd<span class="token punctuation">.</span>set_page_load_timeout<span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span>
<span class="token comment">#https://homicide.latimes.com/age/0/year/all') -- all children under 6</span>
<span class="token comment">#wd.get('https://homicide.latimes.com/neighborhood/compton/cause/gunshot/year/all') -- compton gun homicides</span>
wd<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'https://homicide.latimes.com/gender/female/year/all'</span><span class="token punctuation">)</span> <span class="token comment">#female gun homicides in the county</span>
time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">30</span><span class="token punctuation">)</span>
html_page <span class="token operator">=</span> wd<span class="token punctuation">.</span>page_source
soup <span class="token operator">=</span> bs<span class="token punctuation">(</span>html_page<span class="token punctuation">,</span> <span class="token string">'lxml'</span><span class="token punctuation">)</span>
result1 <span class="token operator">=</span> soup<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'article'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'post-teaser row'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
last_height <span class="token operator">=</span> <span class="token number">0</span>
<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
wd<span class="token punctuation">.</span>execute_script<span class="token punctuation">(</span><span class="token string">"window.scrollTo(0, document.body.scrollHeight)"</span><span class="token punctuation">)</span>
time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
html_page <span class="token operator">=</span> wd<span class="token punctuation">.</span>page_source
soup2 <span class="token operator">=</span> bs<span class="token punctuation">(</span>html_page<span class="token punctuation">,</span> <span class="token string">'lxml'</span><span class="token punctuation">)</span>
time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token comment">#break condition</span>
new_height <span class="token operator">=</span> wd<span class="token punctuation">.</span>execute_script<span class="token punctuation">(</span><span class="token string">"return document.body.scrollHeight"</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> new_height <span class="token operator">==</span> last_height<span class="token punctuation">:</span>
<span class="token keyword">break</span>
last_height <span class="token operator">=</span> new_height
results <span class="token operator">=</span> soup2<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'post-teaser row'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
wd<span class="token punctuation">.</span>quit<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">all_deaths_under_6 <span class="token operator">=</span> result1 <span class="token operator">+</span> results
test <span class="token operator">=</span> soup2<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'post-teaser row'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
test1 <span class="token operator">=</span> soup2<span class="token punctuation">.</span>find_all<span class="token punctuation">(</span><span class="token string">'article'</span><span class="token punctuation">,</span> attrs<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'post-teaser row'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
all_deaths_under_6_div <span class="token operator">=</span> test <span class="token operator">+</span> test1
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">'{} child homicides in LA County'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>all_deaths_under_6_div<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><pre><code>2724 child homicides in LA County
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">all_deaths_under_6_div<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-html" tabindex="0"><code class="language-html is-loaded"> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post-teaser row<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>aside</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>span2 offset1 hidden-phone<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>img</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post-list-thumb<span class="token punctuation">"</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post-list-badge<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
Died on
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>death-date<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>May 28<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>badge-location<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>li</span><span class="token punctuation">></span></span>834 N. Austin Avenue, Inglewood, California 90302 County Los Angeles<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>li</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ul</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>aside</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>article</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>span8 post-list-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>hgroup</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h2</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/post/lauren-alexandria-prarat/<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Lauren Alexandria Prarat, 31<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>a</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h2</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>post-date<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>May 28, 2023<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>hgroup</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>body<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span> Lauren Alexandria Prarat, a 31-year-old <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/race/white/year/all<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>white<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>a</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/gender/female/year/all<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>female<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>a</span><span class="token punctuation">></span></span>, died Sunday, May 28, at 834 N. Austin Avenue, Inglewood, California 90302 County Los Angeles, according to Los Angeles County Medical Examiner-Coroner's records. <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>article</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
</code><button class="copy-code-button">Copy</button></pre></div><div><h2 data-heading="Results" id="Results" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Results</h2></div><div><h4 data-heading="There were 2724 females who were killed by a gun in the county since 2000" id="There_were_2724_females_who_were_killed_by_a_gun_in_the_county_since_2000" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>There were 2724 females who were killed by a gun in the county since 2000</h4></div><div><p>The code above grabbed all of the data that is on the website. The following code parses the text so that it can be stored as data in a data frame. To do so, it is necessary to find all of the unlinked lists (ul's) with class label 'badge-location' to store the child's name, death date, spatial coordinates (which are located in the tag for the image source, as well as the race, gender, age and cause of death listed by the medical-examiner, coroner. Finally, in order to perform textual analysis (e.g., Latent Dirichlet Allocation modeling) the code grabs the description of the details surrounding the death as well as the LA Times reporting of the death.</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">BASE_URL <span class="token operator">=</span> <span class="token string">'https://homicide.latimes.com/'</span>
url <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
uls <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
clist <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
cname <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
b <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
c <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
d <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
e <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
f <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
g <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
j<span class="token operator">=</span><span class="token number">0</span>
<span class="token keyword">for</span> r <span class="token keyword">in</span> all_deaths_under_6_div<span class="token punctuation">:</span>
<span class="token comment">#Find all ul with class: badge-location</span>
containers <span class="token operator">=</span> r<span class="token punctuation">.</span>findAll<span class="token punctuation">(</span><span class="token string">"ul"</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token string">"class"</span> <span class="token punctuation">:</span> <span class="token keyword">lambda</span> L<span class="token punctuation">:</span> L <span class="token keyword">and</span> L<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">'badge-location'</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> container <span class="token keyword">in</span> containers<span class="token punctuation">:</span>
c_names <span class="token operator">=</span> containers<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>findAll<span class="token punctuation">(</span><span class="token string">"li"</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>c_names<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
c_name <span class="token operator">=</span> c_names<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
c_mod_name <span class="token operator">=</span> c_name<span class="token punctuation">.</span>text<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">)</span>
c_mod_name <span class="token operator">=</span> <span class="token punctuation">[</span>item<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span> <span class="token keyword">for</span> item <span class="token keyword">in</span> c_mod_name<span class="token punctuation">]</span>
c_mod_name <span class="token operator">=</span> <span class="token punctuation">[</span>x<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token string">' '</span><span class="token punctuation">)</span> <span class="token keyword">for</span> x <span class="token keyword">in</span> c_mod_name<span class="token punctuation">]</span>
i <span class="token operator">+=</span><span class="token number">1</span>
cname <span class="token operator">=</span> c_mod_name
death_date <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> <span class="token string">'death-date'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> <span class="token string">'death-date'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">else</span> <span class="token string">''</span>
name <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"h2"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text <span class="token keyword">if</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"h2"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">else</span> <span class="token string">''</span>
name <span class="token operator">=</span> name<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
coords <span class="token operator">=</span> r<span class="token punctuation">.</span>img<span class="token punctuation">[</span><span class="token string">'src'</span><span class="token punctuation">]</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span>
<span class="token keyword">except</span> IndexError<span class="token punctuation">:</span>
coords <span class="token operator">=</span> <span class="token string">''</span>
<span class="token keyword">pass</span>
<span class="token keyword">except</span> TypeError<span class="token punctuation">:</span>
coords <span class="token operator">=</span> <span class="token string">''</span>
<span class="token keyword">pass</span>
post_date <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token string">'post-date'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">,</span> <span class="token string">'post-date'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">else</span> <span class="token string">''</span>
race <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span> href<span class="token operator">=</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">"race"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span> href<span class="token operator">=</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">"race"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">else</span> <span class="token string">''</span>
gender <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span> href<span class="token operator">=</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">"gender"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span> href<span class="token operator">=</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">"gender"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">else</span> <span class="token string">''</span>
cause <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span> href<span class="token operator">=</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">"cause"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">,</span> href<span class="token operator">=</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">"cause"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">else</span> <span class="token string">''</span>
s_descript <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> <span class="token string">'body'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'div'</span><span class="token punctuation">,</span> <span class="token string">'body'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">else</span> <span class="token string">''</span>
a <span class="token operator">=</span> r<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> href<span class="token operator">=</span>re<span class="token punctuation">.</span><span class="token builtin">compile</span><span class="token punctuation">(</span><span class="token string">"post"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
url <span class="token operator">=</span> urljoin<span class="token punctuation">(</span>BASE_URL<span class="token punctuation">,</span> a<span class="token punctuation">.</span>attrs<span class="token punctuation">[</span><span class="token string">'href'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
soup <span class="token operator">=</span> bs<span class="token punctuation">(</span>urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>urlopen<span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
title <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'section'</span><span class="token punctuation">,</span> class_<span class="token operator">=</span><span class="token string">"body"</span><span class="token punctuation">)</span>
article_text <span class="token operator">=</span> <span class="token string">''</span>
article <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'section'</span><span class="token punctuation">,</span> class_<span class="token operator">=</span><span class="token string">"body"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>findAll<span class="token punctuation">(</span><span class="token string">'p'</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> element <span class="token keyword">in</span> article<span class="token punctuation">:</span>
article_text <span class="token operator">+=</span> <span class="token string">'\n'</span> <span class="token operator">+</span> <span class="token string">''</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>element<span class="token punctuation">.</span>findAll<span class="token punctuation">(</span>text <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
article_text <span class="token operator">=</span> article_text<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span>
data <span class="token operator">=</span> soup<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">'ul'</span><span class="token punctuation">,</span> class_<span class="token operator">=</span><span class="token string">"aspects"</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> d <span class="token keyword">in</span> data<span class="token punctuation">:</span>
lis <span class="token operator">=</span> data<span class="token punctuation">.</span>findAll<span class="token punctuation">(</span><span class="token string">'li'</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> k <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
aname <span class="token operator">=</span> lis<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>
t <span class="token operator">=</span> <span class="token punctuation">[</span>lis<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span> lis<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span>lis<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span>lis<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span>lis<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span>
lis<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">,</span> lis<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">.</span>text<span class="token punctuation">]</span>
myString <span class="token operator">=</span> <span class="token string">"::"</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>t <span class="token punctuation">)</span>
myString<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">' '</span><span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">)</span>
a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">,</span>d<span class="token punctuation">,</span>e<span class="token punctuation">,</span>f<span class="token punctuation">,</span>g <span class="token operator">=</span> myString<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'::'</span><span class="token punctuation">)</span>
k <span class="token operator">+=</span><span class="token number">1</span>
<span class="token keyword">except</span> IndexError<span class="token punctuation">:</span>
<span class="token keyword">pass</span>
clist<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">(</span>cname<span class="token punctuation">,</span> death_date<span class="token punctuation">,</span> post_date<span class="token punctuation">,</span> name<span class="token punctuation">,</span> race<span class="token punctuation">,</span> gender<span class="token punctuation">,</span>
cause<span class="token punctuation">,</span> coords<span class="token punctuation">,</span> s_descript<span class="token punctuation">,</span> article_text<span class="token punctuation">,</span> c<span class="token punctuation">,</span> d<span class="token punctuation">,</span> e<span class="token punctuation">,</span> f<span class="token punctuation">,</span> g<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">)</span>
j<span class="token operator">+=</span><span class="token number">1</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><h4 data-heading="In order to see the article text (stored in position 9) for the 10th record type the following:" id="In_order_to_see_the_article_text_(stored_in_position_9)_for_the_10th_record_type_the_following:" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>In order to see the article text (stored in position 9) for the 10th record type the following:</h4></div><div><p>This returns the text narrative associated with row 564</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">clist<span class="token punctuation">[</span><span class="token number">564</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-html" tabindex="0"><code class="language-html is-loaded">'Maria Urriola De Pastore, a 69-year-old Latina, died Monday, March 27, after being beaten in the 3600 block of West 106th Street in Inglewood, according to Los Angeles County coroner’s records.\xa0Inglewood police responded\xa0at 12:45 p.m.\xa0to a call about a fight at a house, according to coroner’s records.\xa0They found Pastore seriously wounded. Her husband, Genaro Pastore, was bleeding from his face and unresponsive, records show.The Pastores were transported to the hospital, where\xa0Genaro Pastore, 64,\xa0was pronounced dead at 1:18 p.m., according to coroner’s records.Maria Pastore\xa0arrived at the hospital in critical condition and fell\xa0into a coma. She died at 1:58 p.m.\xa0March 27,\xa0coroner’s records show.Detectives said the couple was attacked by their daughter’s ex-boyfriend, Edward Fernando Ruiz, 33. On the day of Genaro Pastore’s death, Ruiz went to his ex-girlfriend’s house and attacked her, then fled before police arrived. While the woman was transported to a hospital, Ruiz returned and beat her parents, police said.\xa0Later that day, officers found Ruiz hiding on a nearby rooftop. After a two-hour standoff, he climbed down and was arrested.\xa0Prosecutors charged Ruiz with two counts of murder, as well as one count each of resisting an officer, injuring a child’s parents after a prior conviction and violation of a court order.\xa0On April 3, Ruiz’s attorney questioned his client’s\xa0sanity, and Ruiz was sent for a mental health evaluation. He is scheduled to return to court Sept. 6 for results of the exam.\xa0Contact the Homicide Report. Follow @jeromercampbell and @latimeshomicide on Twitter.'
</code><button class="copy-code-button">Copy</button></pre></div><div><p>This woman was attacked <strong><em>by her daughter’s ex-boyfriend</em></strong>, Edward Fernando Ruiz, 33. <strong>The circumstances surrounding the death are clear:</strong> On the day of her death, her daughter's ex-boyfriend went to her daughter's house and attacked her daughter before returning to beat her and her husband, both of whom died as a result of the attack.</p></div><div><p>The data needs to be cleaned and preprocessed prior to visualization. The code below cleans a few of the columns that will serve as preliminary analyses.</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">df <span class="token operator">=</span> pd<span class="token punctuation">.</span>DataFrame<span class="token punctuation">(</span>clist<span class="token punctuation">,</span> columns<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'c_name'</span><span class="token punctuation">,</span> <span class="token string">'death_date'</span><span class="token punctuation">,</span> <span class="token string">'post_date'</span><span class="token punctuation">,</span> <span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'race'</span><span class="token punctuation">,</span> <span class="token string">'gender'</span><span class="token punctuation">,</span> <span class="token string">'cause'</span><span class="token punctuation">,</span> <span class="token string">'coords'</span><span class="token punctuation">,</span> <span class="token string">'s_descript'</span><span class="token punctuation">,</span> <span class="token string">'article_text'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'f'</span><span class="token punctuation">,</span> <span class="token string">'g'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">.</span>post_date<span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'Posted'</span> <span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'causes'</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">.</span>e<span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'Cause:'</span> <span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'brace'</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">.</span>f<span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'Race/Ethnicity:'</span> <span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'hood'</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'href'</span><span class="token punctuation">,</span><span class="token string">''</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'Jan. 18, 2007, noon'</span><span class="token punctuation">,</span> <span class="token string">'Jan. 18, 2007'</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'Oct. 10, 2007, noon'</span><span class="token punctuation">,</span> <span class="token string">'Oct. 10, 2007'</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span> <span class="token operator">=</span> pd<span class="token punctuation">.</span>to_datetime<span class="token punctuation">(</span>df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token string">'year'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> df<span class="token punctuation">[</span><span class="token string">'month'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> df<span class="token punctuation">[</span><span class="token string">'day'</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">apply</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">.</span>year<span class="token punctuation">)</span><span class="token punctuation">,</span> df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">apply</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">.</span>month<span class="token punctuation">)</span><span class="token punctuation">,</span> df<span class="token punctuation">[</span><span class="token string">'post_date'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">apply</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">.</span>day<span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'y'</span><span class="token punctuation">,</span> <span class="token string">'x'</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token string">'coords'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> expand <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'x'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token string">'x'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">')'</span><span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> expand <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span>
df<span class="token punctuation">.</span><span class="token builtin">apply</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span><span class="token string">'%s, %s'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>x<span class="token punctuation">[</span><span class="token string">'death_date'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>x<span class="token punctuation">[</span><span class="token string">'year'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
df<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'d1'</span><span class="token punctuation">,</span> <span class="token string">'y'</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token string">'y'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">'('</span><span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> expand <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><p>Save file for later processing</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">df<span class="token punctuation">.</span>to_csv<span class="token punctuation">(</span><span class="token string">'female_shot_all.csv'</span><span class="token punctuation">,</span> sep<span class="token operator">=</span><span class="token string">','</span><span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><h3 data-heading="Now we can visualize the data" id="Now_we_can_visualize_the_data" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Now we can visualize the data</h3></div><div><p>First, filter and clean some of the data for the plot</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">df <span class="token operator">=</span> df<span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'causes'</span><span class="token punctuation">,</span><span class="token string">'x'</span><span class="token punctuation">,</span> <span class="token string">'y'</span><span class="token punctuation">,</span> <span class="token string">'brace'</span><span class="token punctuation">]</span><span class="token punctuation">]</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">df1 <span class="token operator">=</span> df<span class="token punctuation">[</span>df<span class="token punctuation">[</span><span class="token string">'brace'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">"Black"</span><span class="token punctuation">)</span> <span class="token operator">|</span> df<span class="token punctuation">[</span><span class="token string">'brace'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">'White'</span><span class="token punctuation">)</span><span class="token operator">|</span> df<span class="token punctuation">[</span><span class="token string">'brace'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">'Latino'</span><span class="token punctuation">)</span><span class="token operator">|</span> df<span class="token punctuation">[</span><span class="token string">'brace'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">'Asian'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
df1 <span class="token operator">=</span> df<span class="token punctuation">[</span>df<span class="token punctuation">[</span><span class="token string">'causes'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">"Blunt force"</span><span class="token punctuation">)</span> <span class="token operator">|</span> df<span class="token punctuation">[</span><span class="token string">'causes'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">'Gunshot'</span><span class="token punctuation">)</span><span class="token operator">|</span> df<span class="token punctuation">[</span><span class="token string">'causes'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">'Stabbing'</span><span class="token punctuation">)</span><span class="token operator">|</span> df<span class="token punctuation">[</span><span class="token string">'causes'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token builtin">str</span><span class="token punctuation">.</span>contains<span class="token punctuation">(</span><span class="token string">'Strangled'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
df1
</code><button class="copy-code-button">Copy</button></pre></div><div><div>
</div></div><div><pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code><button class="copy-code-button">Copy</button></pre></div><div><table class="dataframe" border="1">
<thead>
<tr style="text-align: right;">
<th></th>
<th>causes</th>
<th>x</th>
<th>y</th>
<th>brace</th>
</tr>
</thead>
<tbody>
<tr>
<th>102</th>
<td>Gunshot</td>
<td>33.98166149582505</td>
<td>-118.30918538353123</td>
<td>White</td>
</tr>
<tr>
<th>103</th>
<td>Stabbing</td>
<td>33.77912959584337</td>
<td>-118.19406418354725</td>
<td>Black</td>
</tr>
<tr>
<th>104</th>
<td>Gunshot</td>
<td>33.8924981958331</td>
<td>-118.29064368353382</td>
<td>Black</td>
</tr>
<tr>
<th>105</th>
<td>Blunt force</td>
<td>34.056642995818336</td>
<td>-118.23177148354199</td>
<td>Black</td>
</tr>
<tr>
<th>106</th>
<td>Gunshot</td>
<td>33.892855995833074</td>
<td>-118.24874548353962</td>
<td>Latino</td>
</tr>
<tr>
<th>...</th>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<th>2706</th>
<td>Gunshot</td>
<td>33.897855996706</td>
<td>-118.2340869835372</td>
<td>Latino</td>
</tr>
<tr>
<th>2707</th>
<td>Gunshot</td>
<td>34.221928</td>
<td>-118.454991</td>
<td>Latino</td>
</tr>
<tr>
<th>2708</th>
<td>Gunshot</td>
<td>None</td>
<td>None</td>
<td>White</td>
</tr>
<tr>
<th>2709</th>
<td>Gunshot</td>
<td>None</td>
<td>None</td>
<td>White</td>
</tr>
<tr>
<th>2710</th>
<td>Gunshot</td>
<td>None</td>
<td>None</td>
<td>White</td>
</tr>
</tbody>
</table>
<p>2265 rows × 4 columns</p>
</div><div><h4 data-heading="Within Jupyter we can run R code" id="Within_Jupyter_we_can_run_R_code" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Within Jupyter we can run R code</h4></div><div><p>This uses ggplot to make a bar chart of causes of death by race of the female</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded"><span class="token keyword">from</span> plotnine <span class="token keyword">import</span> <span class="token operator">*</span>
<span class="token punctuation">(</span>ggplot<span class="token punctuation">(</span>df1<span class="token punctuation">)</span> <span class="token operator">+</span>
aes<span class="token punctuation">(</span>x<span class="token operator">=</span><span class="token string">'causes'</span><span class="token punctuation">,</span> fill <span class="token operator">=</span> <span class="token string">'brace'</span><span class="token punctuation">)</span> <span class="token operator">+</span>
geom_bar<span class="token punctuation">(</span>alpha<span class="token operator">=</span><span class="token number">.4</span><span class="token punctuation">,</span> position<span class="token operator">=</span><span class="token string">"identity"</span><span class="token punctuation">)</span> <span class="token operator">+</span>
ggtitle<span class="token punctuation">(</span><span class="token string">'Cause of Death'</span><span class="token punctuation">)</span> <span class="token operator">+</span>
labs<span class="token punctuation">(</span>x <span class="token operator">=</span> <span class="token string">"Cause of Death"</span><span class="token punctuation">,</span> y <span class="token operator">=</span> <span class="token string">"Count"</span><span class="token punctuation">,</span> fill <span class="token operator">=</span> <span class="token string">"Race/Ethnicity"</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><p> </p><div alt="png" src="output_18_0.png" class="internal-embed media-embed image-embed is-loaded"><img alt="png" src="output_18_0.png"></div><p></p></div><div><p>From the chart above, the most common cause of death is gunshot, and Latina and Black women are numerically more likely to due by gun shot. </p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">df_complete <span class="token operator">=</span> df<span class="token punctuation">.</span>dropna<span class="token punctuation">(</span>subset<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'x'</span><span class="token punctuation">,</span> <span class="token string">'y'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
df_complete<span class="token punctuation">[</span><span class="token string">'y'</span><span class="token punctuation">]</span> <span class="token operator">=</span> pd<span class="token punctuation">.</span>to_numeric<span class="token punctuation">(</span>df_complete<span class="token punctuation">[</span><span class="token string">'y'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> errors<span class="token operator">=</span><span class="token string">'coerce'</span><span class="token punctuation">)</span>
df_complete<span class="token punctuation">[</span><span class="token string">'x'</span><span class="token punctuation">]</span> <span class="token operator">=</span> pd<span class="token punctuation">.</span>to_numeric<span class="token punctuation">(</span>df_complete<span class="token punctuation">[</span><span class="token string">'x'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> errors<span class="token operator">=</span><span class="token string">'coerce'</span><span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><h3 data-heading="Mapping the results" id="Mapping_the_results" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Mapping the results</h3></div><div><h4 data-heading="Acquire shapefiles and transform the data rame into a spatial data frame" id="Acquire_shapefiles_and_transform_the_data_rame_into_a_spatial_data_frame" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Acquire shapefiles and transform the data rame into a spatial data frame</h4></div><div><p>We create a spatial points data frame and download neighborhood boundaries from the LA Times website. Then we can get a sense of where females are relatively more likely to die.</p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded"><span class="token keyword">import</span> geopandas <span class="token keyword">as</span> gpd
<span class="token keyword">from</span> geopandas <span class="token keyword">import</span> GeoDataFrame
<span class="token keyword">from</span> shapely<span class="token punctuation">.</span>geometry <span class="token keyword">import</span> Point
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">shape<span class="token operator">=</span>gpd<span class="token punctuation">.</span>read_file<span class="token punctuation">(</span><span class="token string">'/Users/gbarboza/Downloads/la-county-neighborhoods-v5/l.a. county neighborhood (v5).shp'</span><span class="token punctuation">)</span>
geometry <span class="token operator">=</span> <span class="token punctuation">[</span>Point<span class="token punctuation">(</span>xy<span class="token punctuation">)</span> <span class="token keyword">for</span> xy <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>df_complete<span class="token punctuation">.</span>y<span class="token punctuation">,</span> df_complete<span class="token punctuation">.</span>x<span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token comment">#df_complete = df_complete.drop(['x', 'y'], axis=1)</span>
crs <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'init'</span><span class="token punctuation">:</span> <span class="token string">'epsg:4326'</span><span class="token punctuation">}</span>
gdf <span class="token operator">=</span> GeoDataFrame<span class="token punctuation">(</span>df_complete<span class="token punctuation">,</span> crs<span class="token operator">=</span>crs<span class="token punctuation">,</span> geometry<span class="token operator">=</span>geometry<span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded"><span class="token keyword">import</span> matplotlib
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>dates <span class="token keyword">as</span> mdates
<span class="token keyword">from</span> matplotlib<span class="token punctuation">.</span>ticker <span class="token keyword">import</span> StrMethodFormatter
<span class="token keyword">import</span> matplotlib<span class="token punctuation">.</span>pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> seaborn <span class="token keyword">as</span> sns
</code><button class="copy-code-button">Copy</button></pre></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded"><span class="token keyword">import</span> os
<span class="token keyword">from</span> mpl_toolkits<span class="token punctuation">.</span>basemap <span class="token keyword">import</span> Basemap
fig <span class="token operator">=</span> plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#Let's create a map centered over Los Angeles, CA (34.0522° N, 118.2437° W) and extending 10 degrees W-E and 5 degrees N-S</span>
my_coords <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">34.587234</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">118.126193</span><span class="token punctuation">]</span>
<span class="token builtin">map</span> <span class="token operator">=</span> Basemap<span class="token punctuation">(</span>projection<span class="token operator">=</span><span class="token string">'merc'</span><span class="token punctuation">,</span>llcrnrlon<span class="token operator">=</span><span class="token operator">-</span><span class="token number">121.</span><span class="token punctuation">,</span>llcrnrlat<span class="token operator">=</span><span class="token number">33.</span><span class="token punctuation">,</span>urcrnrlon<span class="token operator">=</span><span class="token operator">-</span><span class="token number">116.</span><span class="token punctuation">,</span>urcrnrlat<span class="token operator">=</span><span class="token number">35.</span><span class="token punctuation">,</span>resolution<span class="token operator">=</span><span class="token string">'i'</span><span class="token punctuation">)</span> <span class="token comment"># projection, lat/lon extents and resolution of polygons to draw</span>
<span class="token comment"># resolutions: c - crude, l - low, i - intermediate, h - high, f - full</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>drawcoastlines<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>drawstates<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>drawcountries<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>drawlsmask<span class="token punctuation">(</span>land_color<span class="token operator">=</span><span class="token string">'Linen'</span><span class="token punctuation">,</span> ocean_color<span class="token operator">=</span><span class="token string">'#CCFFFF'</span><span class="token punctuation">)</span> <span class="token comment"># can use HTML names or codes for colors</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>drawcounties<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># you can even add counties (and other shapefiles!)</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>drawmapboundary<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># Plot the data</span>
mxy <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span>gdf<span class="token punctuation">[</span><span class="token string">"y"</span><span class="token punctuation">]</span><span class="token punctuation">.</span>tolist<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> gdf<span class="token punctuation">[</span><span class="token string">"x"</span><span class="token punctuation">]</span><span class="token punctuation">.</span>tolist<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>scatter<span class="token punctuation">(</span>mxy<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> mxy<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> s<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> c<span class="token operator">=</span><span class="token string">"#1292db"</span><span class="token punctuation">,</span> lw<span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">,</span> alpha<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> zorder<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
x<span class="token punctuation">,</span> y <span class="token operator">=</span> <span class="token builtin">map</span><span class="token punctuation">(</span>my_coords<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> my_coords<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
zoom_scale <span class="token operator">=</span> <span class="token number">1</span>
<span class="token builtin">map</span><span class="token punctuation">.</span>plot<span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">,</span>marker<span class="token operator">=</span><span class="token string">'D'</span><span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">,</span> markersize<span class="token operator">=</span><span class="token number">14</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>text<span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token string">'Gabriel Fernandez'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">"Child Homicide Locations (2000-2020)"</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>savefig<span class="token punctuation">(</span><span class="token string">'gabriel_fernandez.png'</span><span class="token punctuation">,</span> <span class="token builtin">format</span><span class="token operator">=</span><span class="token string">'png'</span><span class="token punctuation">,</span> dpi<span class="token operator">=</span><span class="token number">500</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#draw map</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><p></p><div alt="png" src="output_24_0.png" class="internal-embed media-embed image-embed is-loaded"><img alt="png" src="output_24_0.png"></div><p></p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">f<span class="token punctuation">,</span> ax <span class="token operator">=</span> plt<span class="token punctuation">.</span>subplots<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
f<span class="token punctuation">.</span>patch<span class="token punctuation">.</span>set_facecolor<span class="token punctuation">(</span><span class="token string">'xkcd:white'</span><span class="token punctuation">)</span>
shape<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>ax<span class="token operator">=</span>ax<span class="token punctuation">,</span> color<span class="token operator">=</span><span class="token string">'white'</span><span class="token punctuation">,</span> edgecolor<span class="token operator">=</span><span class="token string">'gray'</span> <span class="token punctuation">,</span> alpha<span class="token operator">=</span><span class="token number">0.8</span><span class="token punctuation">,</span> linewidth<span class="token operator">=</span><span class="token number">.9</span><span class="token punctuation">)</span>
sns<span class="token punctuation">.</span>kdeplot<span class="token punctuation">(</span>gdf<span class="token punctuation">[</span><span class="token string">'y'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> gdf<span class="token punctuation">[</span><span class="token string">'x'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> n_levels<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">,</span> cmap<span class="token operator">=</span><span class="token string">"viridis"</span><span class="token punctuation">,</span> shade<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> shade_lowest<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> ax<span class="token operator">=</span>ax<span class="token punctuation">,</span> linewidths<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> bw_adjust<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span> alpha<span class="token operator">=</span><span class="token number">.9</span><span class="token punctuation">,</span> kernel<span class="token operator">=</span><span class="token string">'gau'</span><span class="token punctuation">,</span>gridsize<span class="token operator">=</span><span class="token number">150</span><span class="token punctuation">,</span> bw<span class="token operator">=</span><span class="token string">'scott'</span><span class="token punctuation">,</span>cut<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>cbar<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
ax <span class="token operator">=</span> gdf<span class="token punctuation">.</span>plot<span class="token punctuation">(</span>ax<span class="token operator">=</span>ax<span class="token punctuation">,</span>marker<span class="token operator">=</span><span class="token string">'o'</span><span class="token punctuation">,</span> color<span class="token operator">=</span><span class="token string">'r'</span><span class="token punctuation">,</span> markersize<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> alpha<span class="token operator">=</span><span class="token number">0.3</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'equal'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><p></p><div alt="png" src="output_25_1.png" class="internal-embed media-embed image-embed is-loaded"><img alt="png" src="output_25_1.png"></div><p></p></div><div><pre class="language-python" tabindex="0"><code class="language-python is-loaded">graph <span class="token operator">=</span> sns<span class="token punctuation">.</span>jointplot<span class="token punctuation">(</span>x <span class="token operator">=</span> <span class="token string">'y'</span><span class="token punctuation">,</span>y <span class="token operator">=</span> <span class="token string">'x'</span><span class="token punctuation">,</span>data <span class="token operator">=</span> gdf<span class="token punctuation">,</span>kind <span class="token operator">=</span> <span class="token string">'hex'</span><span class="token punctuation">,</span> height <span class="token operator">=</span> <span class="token number">12</span><span class="token punctuation">)</span>
</code><button class="copy-code-button">Copy</button></pre></div><div><p></p><div alt="png" src="output_26_0.png" class="internal-embed media-embed image-embed is-loaded"><img alt="png" src="output_26_0.png"></div><p></p></div><div><h2 data-heading="Summary" id="Summary" style="display: flex;"><div class="heading-collapse-indicator collapse-indicator collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div>Summary</h2></div><div><p>The maps above show the locations of child homicides across the county. The maps show a strong spatial clustering in the city of Los Angeles. The number of child homicides decreased over the years, however it is possible that we see an increase in child deaths due to maltreatment as a result of the COVID-19 outbreak. These data should be monitored so that preventive interventions can be targeted to the communities that are in most need.</p></div><div class="mod-footer"><div class="embedded-backlinks" style="display: none;"></div></div></div></div></div><div class="sidebar-right sidebar"><div class="sidebar-content"><span class="sidebar-section-header">Interactive Graph</span><div class="graph-view-placeholder">
<div class="graph-view-container">
<div class="graph-icon graph-expand" role="button" aria-label="Expand" data-tooltip-position="top"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon lucide-arrow-up-right"><line x1="7" y1="17" x2="17" y2="7"></line><polyline points="7 7 17 7 17 17"></polyline></svg></div>
<canvas id="graph-canvas" width="512px" height="512px"></canvas>
</div>
</div><div class="tree-container outline-tree" data-depth="0"><div class="tree-header"><span class="sidebar-section-header">Table Of Contents</span><button class="clickable-icon collapse-tree-button"><iconify-icon icon="ph:arrows-in-line-horizontal-bold" width="18px" height="18px" rotate="90deg" color="currentColor"></iconify-icon></button></div><div class="tree-scroll-area"><div class="tree-item mod-tree-heading" data-depth="1"><div class="tree-item-contents"><a class="tree-item-link" href="#Mining_Homicides_in_Los_Angeles_County"><span class="tree-item-title">Mining Homicides in Los Angeles County</span></a></div><div class="tree-item-children"><div class="tree-item mod-tree-heading" data-depth="2"><div class="tree-item-contents"><a class="tree-item-link" href="#Background"><span class="tree-item-title">Background</span></a></div><div class="tree-item-children"></div></div><div class="tree-item mod-tree-heading" data-depth="2"><div class="tree-item-contents"><a class="tree-item-link" href="#My_Work"><span class="tree-item-title">My Work</span></a></div><div class="tree-item-children"></div></div><div class="tree-item mod-tree-heading" data-depth="2"><div class="tree-item-contents"><a class="tree-item-link" href="#The_Problem"><span class="tree-item-title">The Problem</span></a></div><div class="tree-item-children"></div></div><div class="tree-item mod-tree-heading" data-depth="2"><div class="tree-item-contents"><a class="tree-item-link" href="#Preliminaries"><span class="tree-item-title">Preliminaries</span></a></div><div class="tree-item-children"></div></div><div class="tree-item mod-tree-heading" data-depth="2"><div class="tree-item-contents"><a class="tree-item-link" href="#Data_Extraction"><span class="tree-item-title">Data Extraction</span></a></div><div class="tree-item-children"></div></div><div class="tree-item mod-tree-heading mod-collapsible" data-depth="2"><div class="tree-item-contents"><div class="tree-item-icon collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div><a class="tree-item-link" href="#Results"><span class="tree-item-title">Results</span></a></div><div class="tree-item-children"><div class="tree-item mod-tree-heading" data-depth="4"><div class="tree-item-contents"><a class="tree-item-link" href="#There_were_2724_females_who_were_killed_by_a_gun_in_the_county_since_2000"><span class="tree-item-title">There were 2724 females who were killed by a gun in the county since 2000</span></a></div><div class="tree-item-children"></div></div><div class="tree-item mod-tree-heading" data-depth="4"><div class="tree-item-contents"><a class="tree-item-link" href="#In_order_to_see_the_article_text_(stored_in_position_9)_for_the_10th_record_type_the_following:"><span class="tree-item-title">In order to see the article text (stored in position 9) for the 10th record type the following:</span></a></div><div class="tree-item-children"></div></div><div class="tree-item mod-tree-heading mod-collapsible" data-depth="3"><div class="tree-item-contents"><div class="tree-item-icon collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div><a class="tree-item-link" href="#Now_we_can_visualize_the_data"><span class="tree-item-title">Now we can visualize the data</span></a></div><div class="tree-item-children"><div class="tree-item mod-tree-heading" data-depth="4"><div class="tree-item-contents"><a class="tree-item-link" href="#Within_Jupyter_we_can_run_R_code"><span class="tree-item-title">Within Jupyter we can run R code</span></a></div><div class="tree-item-children"></div></div></div></div><div class="tree-item mod-tree-heading mod-collapsible" data-depth="3"><div class="tree-item-contents"><div class="tree-item-icon collapse-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="svg-icon right-triangle"><path d="M3 8L12 17L21 8"></path></svg></div><a class="tree-item-link" href="#Mapping_the_results"><span class="tree-item-title">Mapping the results</span></a></div><div class="tree-item-children"><div class="tree-item mod-tree-heading" data-depth="4"><div class="tree-item-contents"><a class="tree-item-link" href="#Acquire_shapefiles_and_transform_the_data_rame_into_a_spatial_data_frame"><span class="tree-item-title">Acquire shapefiles and transform the data rame into a spatial data frame</span></a></div><div class="tree-item-children"></div></div></div></div></div></div><div class="tree-item mod-tree-heading" data-depth="2"><div class="tree-item-contents"><a class="tree-item-link" href="#Summary"><span class="tree-item-title">Summary</span></a></div><div class="tree-item-children"></div></div></div></div></div></div></div></div></div></body></html>