Fast First Python Notebook

By • ONA22 in Zoom

Recording

Show the timestamped transcript
  1. All right, well, we've reached our start time.
  2. Thank you, everybody, for your patience.
  3. Welcome to First Python Notebook.
  4. Thank you so much for signing up and coming.
  5. This is going to be a course that introduces you
  6. to the basics, the beginnings of data analysis.
  7. For news using the Python computer programming language,
  8. the Jupyter Notebook,
  9. and the Pandas Data Analysis Toolkit,
  10. these are pieces of nerdy jargon that we will explain,
  11. that we will demystify,
  12. and that we will answer all of your questions about.
  13. Over the next, I hope, kind of two to maybe three hours.
  14. I don't know, maybe we'll be quick.
  15. I don't know, we'll find out.
  16. But over the next chunk of time,
  17. we're all gonna work through an example of those tools
  18. being put to use to do a story.
  19. In fact, a story I worked on myself
  20. that we'll talk more about in a bit.
  21. And we're gonna go through it step by step.
  22. And along the way, learn a lot of the fundamentals
  23. that are used in all kinds of different stories,
  24. not just this one.
  25. And that hopefully can serve you in the future
  26. if you're interested in using them.
  27. And the things we're covering also are useful
  28. for a lot of things besides kind of investigative stories
  29. or whatever, the skills we'll cover could be used
  30. for analytics work, engagement work,
  31. all kinds of very important things
  32. that happen in newsrooms as well.
  33. And really, the tools weren't invented
  34. for what our example used them for either.
  35. So there's a lot of different stuff
  36. that can be done with it.
  37. And we're really gonna try to cover the fundamentals
  38. of what is really the goal.
  39. So that's the deal.
  40. Oh, by the way, my name's Ben Welsh.
  41. I'm coming to you live from Little Tokyo
  42. in downtown Los Angeles at the corner of 2nd and Central
  43. in the office that my wife and I worked from.
  44. I have been a data journalist for about, gosh,
  45. getting close to 20 years.
  46. I'm currently a visiting Los Angeles Times reporter
  47. at a project at Stanford University called Big Local News,
  48. which is a nonprofit university-based effort
  49. to try to help local newsrooms do more with data
  50. by gathering and refining big records and databases
  51. that people might not have time to do on their own.
  52. Doing that at scale so we can have it for the whole country
  53. or as much of it as possible,
  54. so it can be useful to reporters
  55. in a lot of different places.
  56. And then coming up with tools
  57. that make it easier for them to do that.
  58. And so you can learn more about it
  59. if you go to biglocalnews.org, that's me.
  60. Ryan, you wanna introduce yourself?
  61. Yeah, hey all, I'm Ryan,
  62. and I am joining you from my office in Spokane, Washington,
  63. which is also in my house.
  64. But I've lived here in Spokane for quite a while
  65. and was in local news at the paper here
  66. for much of my journalism career.
  67. And that's kinda where I first started getting into Python.
  68. We used it to both, to develop the website
  69. that we use to publish our news online,
  70. but also to start doing data analysis and mapping projects
  71. and a lot of the really cool stuff
  72. that you get to work on in data journalism.
  73. I left that role to work on a project
  74. called Census Reporter, where we also use Python.
  75. It's a website that is designed to make census data
  76. easier for journalists to use.
  77. So if you wanna check out censusreporter.org, that's us.
  78. You can poke around with ACS data.
  79. I think we'll get the new stuff,
  80. the new data release in there sometime
  81. in the next couple of weeks, which will be fun.
  82. So yeah, yeah, thanks Ben.
  83. So yeah, that's a little bit about
  84. where I live here in Spokane.
  85. So I've been using Python for journalism for a long time.
  86. Census Reporter is a project, a labor of love at this point.
  87. We're all volunteers maintaining it.
  88. And my full-time role is with a program called Open News.
  89. And we exist to support people who kind of work
  90. at that intersection of journalism and technology.
  91. We help connect them with resources they need.
  92. If you work in a smaller newsroom
  93. and you don't have access to professional development
  94. or colleagues to help like double-check your work,
  95. there are a lot of programs that we have
  96. particularly designed to help people who work
  97. in journalism and tech connect with resources they need
  98. and also to connect with other people.
  99. So that's one of the reasons why getting to jump on board
  100. for a workshop like this one, I think is really fun
  101. because we got a bunch of people in here
  102. who are learning together
  103. and maybe we'll get to know each other a little bit
  104. and hopefully have a chance to figure out
  105. why you all are really interested in picking up
  106. Python skills for journalism because it's awesome.
  107. I'm super excited that we're gonna get
  108. to learn this stuff together today.
  109. Great, thank you, Ryan.
  110. I really appreciate you coming to help.
  111. All right, so we're ready to begin after that prologue.
  112. So everybody needs to open their web browser
  113. and go to the URL that I've highlighted here on the screen.
  114. That is at bit.ly slash FPN lite, L-I-T-E.
  115. FPN stands for first Python notebook,
  116. which is the name of the class and lite
  117. because we're gonna use it in a tool called Jupyter lite.
  118. I'm gonna talk about more in a minute.
  119. So everybody go to bit.ly slash FPN lite
  120. and that should take you to a page that looks like this one.
  121. Is this what everybody sees?
  122. If you don't see this, please speak up and let us know.
  123. I'm gonna try to bump up the font size on mine
  124. just to maybe make it more legible.
  125. You think that would help, Ryan?
  126. Yeah, I think so.
  127. And I'll just say if Ben is gonna be driving
  128. this Python bus and I am gonna pitch in where I can,
  129. but one of the things that I can do is if you are,
  130. if you're running into any trouble
  131. and you throw something into chat,
  132. then I will ping you back and can hopefully help folks
  133. work through any hurdles they're running into.
  134. So yeah, throw something in there
  135. if there's a question that you have
  136. and I'll be happy to help out.
  137. Okay, great.
  138. Is this getting better, Ryan?
  139. Is that legible?
  140. Yeah, I think so.
  141. Love it.
  142. All right, so what we have here is,
  143. believe it or not, your first Jupyter notebook.
  144. This is it.
  145. The class is gonna teach you Jupyter notebooks
  146. and the class itself is a Jupyter notebook.
  147. Oh my God, it's so meta, right?
  148. So what we have here from top to bottom
  149. is the entire script for our class,
  150. everything we're gonna learn
  151. that we're gonna work through together slowly,
  152. cell by cell or step by step as we go.
  153. All right, and this class is sort of a streamlined version
  154. of something of a longer bootcamp,
  155. also called first Python notebook
  156. that I've been teaching for about five or six years
  157. that usually takes about six hours
  158. and it's one of these things at a conference
  159. where you get a bunch of people in a room
  160. and you just keep coding until you're done, until you drop,
  161. and also teaches a few things we won't cover today
  162. like how to make some charts
  163. and a lot of the fundamentals of campaign finance analysis.
  164. So if you're interested in learning more
  165. after we finish today or you really are curious
  166. about how to do campaign finance analysis,
  167. I would encourage you to, on your own time,
  168. just check out the longer version.
  169. The streamlined version here today
  170. is really intended to cover the essentials of data analysis
  171. and working with Jupyter taking as little time as possible
  172. and so we're gonna move a little more quickly.
  173. All the code we're gonna write has already been written.
  174. There'll be opportunities for you to write more
  175. and to modify it as we go,
  176. but we're gonna move a little faster
  177. than we do in that longer take, all right?
  178. And so that's why I call this fast Python notebook.
  179. Ha ha ha, all right?
  180. And so let's settle all the jargon stuff
  181. before we start, right?
  182. Because there's a lot of dumb terms in computer programming
  183. that get thrown around without getting defined.
  184. I just wanna make sure we're clear
  185. about what these things are.
  186. So Python, what's Python?
  187. I'm guessing you all already know, but just to be clear,
  188. it's a computer programming language, right?
  189. It's a way for you to write instructions
  190. to tell your computer what to do, right?
  191. And to tell it how to manipulate data,
  192. to tell it how to do different things.
  193. And Python can do all kinds of stuff.
  194. It can make music, it can send a rocket ship into space,
  195. it can build a website, and it can analyze data, right?
  196. Among many other things.
  197. And it's called Python
  198. because some nerds decided to call it that
  199. about 30 years ago,
  200. because they really liked Monty Python.
  201. That's the reason, you know?
  202. And there's other programming languages
  203. you may have heard of like Node.js or Ruby
  204. or what's another one?
  205. Gee, go, right?
  206. And these are all sort of competitors
  207. that are just trying to develop ways
  208. to help programmers make computers do things, right?
  209. And so where Python's the library we're using today,
  210. that's piece of jargon one.
  211. Number two is the Jupyter Notebook, right?
  212. The Jupyter Notebook is a browser-based interface.
  213. You're looking at it right now,
  214. which just tries to make it easier
  215. for you to write your Python code.
  216. It has a whole set of windows and boxes and buttons
  217. that just try to make it possible
  218. for you to quickly write, run, remix,
  219. but also then share the code that you write as you go, right?
  220. And it's a free and open source piece of software
  221. that was developed by a guy named Fernando Perez
  222. that's now used by people all around the world,
  223. scientists, scholars, financial tech bros,
  224. and you know, anyone else you could think of
  225. to kind of document and share their programming work,
  226. particularly when it comes to data analysis.
  227. It was initially designed to try to help scientists
  228. write white papers, you know,
  229. to write scientific papers that were reproducible.
  230. So your physics paper, you know,
  231. you have all this math in it,
  232. and it might get published in a journal,
  233. but how do you know the math actually works or is right,
  234. right?
  235. The Notebook is intended to make it reproducible by anyone.
  236. And so that's why a lot of the interface
  237. looks like a scientific paper at times,
  238. and especially when you look at the examples.
  239. Newsrooms use it quite a bit.
  240. I've linked to some examples here
  241. that you can find on the web to read.
  242. If you're interested,
  243. cases of Buzzfeed, the BBC, ProPublica,
  244. the LA Times where I work,
  245. many, many outlets will publish on the web,
  246. the notebooks that demonstrate and reproduce
  247. the data analysis that, you know, underlies their stories.
  248. And this one sort of shows their work
  249. and can help to demonstrate to the audience
  250. that they know what they're doing.
  251. And, you know, is it a really awesome form of transparency?
  252. But it's also, I think, whether you publish it or not,
  253. you know, a lot of notebooks probably just stay private.
  254. It's a way for you to kind of have something
  255. that's reproducible and auditable and like clear
  256. so that, you know, when you're finishing your story
  257. and bulletproofing your work,
  258. it's a lot easier to go back and see everything you did
  259. and make sure you didn't make any mistakes, right?
  260. And it's super valuable for that.
  261. Anytime I do a story that's based on analysis,
  262. I try to do a notebook,
  263. and anytime I can, I try to put it on the web.
  264. So if you're interested in seeing
  265. how other journalists use this tool,
  266. the best way to do it is click some of these links
  267. and just start looking at the notebooks
  268. that journalists have published on GitHub,
  269. which is a website where people share their code.
  270. So that's the Jupyter notebook.
  271. It's like the planet Jupyter with P-Y
  272. because the Y is Python, right?
  273. Though it actually does support other programming languages
  274. now beyond Python, like R, for instance.
  275. So that's the Jupyter notebook.
  276. And then I think our third big piece of jargon
  277. I just want to cover before we start is pandas.
  278. Pandas is a big, gigantic hunk of Python code
  279. that just makes it easier to do data analysis.
  280. You really should think about it as sort of like a tool belt.
  281. It's like a Python tool belt
  282. that allows you to read in data files,
  283. write out data files, sort and filter and group,
  284. and create new columns and all the stuff
  285. that you might do in a spreadsheet tool, for instance,
  286. you're able to do in computer programming code
  287. so that you can work on gigantic large data sets,
  288. but that also so that you could automate
  289. and rerun and reproduce your work more easily
  290. rather than every time you would do something in Excel,
  291. of course you have to point and click and do it, right?
  292. But in a way it's sort of like an invisible spreadsheet
  293. that's inside your computer
  294. that you're manipulating using Python, right?
  295. I think those are all the pieces of jargon,
  296. but occasionally I will just spit one out and not realize it.
  297. And I apologize if I do that.
  298. Just feel free to interrupt and ask
  299. if you don't know what it means, right?
  300. So that's the spiel on what the jargon is
  301. and we'll start coding here in a minute,
  302. but I just wanna throw it out to the audience
  303. in case anybody has any questions about one, the jargon,
  304. but two, just more broadly about these big tools,
  305. Python, Jupyter or pandas, or really anything like that.
  306. Is there anything you wanna know
  307. or any questions you have before we begin?
  308. Feel free to unmute and speak up.
  309. Thank you.
  310. Hearing none, I will continue.
  311. It doesn't hurt my feelings, I promise.
  312. But as I get going, I can kind of get on a roll
  313. and if I'm going off track
  314. or you wanna hear something back
  315. or something doesn't make sense,
  316. just feel free to speak up and interrupt me.
  317. You know what I mean?
  318. I actually would like it.
  319. I think I wanna interact with you guys.
  320. I wanna help you understand things.
  321. I wanna get you where you wanna go.
  322. And so honestly, I welcome interruptions.
  323. So feel free at any time to do so, all right?
  324. Okay, so I think we can start, I guess.
  325. I think I'd like to quickly start
  326. with just the tour of the Jupyter notebook itself,
  327. which we are inside.
  328. We are actually inside it in our browser
  329. thanks to a modified version of Jupyter called Jupyter Lite.
  330. Jupyter Lite is a sort of brand new, easy way
  331. to get one of these notebooks up and running in your browser
  332. without having to install anything on your computer.
  333. One of the most frustrating things
  334. about computer programming is getting started,
  335. just getting everything working.
  336. And Python is pretty bad about this for new people
  337. in terms of just getting it up and going
  338. often requires getting into your terminal,
  339. installing strange tools you may not have experience with.
  340. And it's just kind of a hassle.
  341. So a cool thing about Jupyter Lite
  342. is that it lets you just like, boom,
  343. get it going in your browser with nothing else happening.
  344. However, because it runs in your browser,
  345. it's not as powerful as it might be
  346. if it were installed in your computer
  347. because it doesn't have access to all the gears
  348. that are inside your laptop or whatever computer you have
  349. that are sometimes necessary
  350. when you're working on large data streams.
  351. So I would say if you try out Jupyter today in Jupyter Lite
  352. and you like it and you want to do more with it
  353. or try a story, I'd recommend that you try to install
  354. the full version to do at that point
  355. to really get serious about doing the work
  356. because Lite is, I think at this stage,
  357. more for experimentation, okay?
  358. There's links right here in the full edition
  359. of First Bite, the notebook,
  360. that can show you how to do that.
  361. I would really recommend for someone
  362. who's just getting started a tool called Jupyter Desktop,
  363. which you can find explained in that documentation.
  364. And it's a really easy kind of like executable thing
  365. you can quickly install on your computer
  366. and it's just up and running
  367. without having to use the terminal or do anything too nerdy.
  368. So Jupyter Desktop is what I would recommend in that case.
  369. And regardless of whether you have Lite or desktop
  370. or whatever, the interface is gonna look the same, right?
  371. It just will or won't be in kind of on the web, right?
  372. Where if you use desktop, it'll be on your computer
  373. and it won't be on the web, right?
  374. But once you're in here, it'll look about the same.
  375. And the interface I think is probably pretty self-explanatory
  376. to people who have experience with computers,
  377. which I suspect you all do.
  378. But just to kind of go over quickly,
  379. we see here on the left is sort of a file directory.
  380. This is just as you might have in a text editor,
  381. just a space where it says,
  382. this is the folder in your computer
  383. where the environment is currently based.
  384. And here's the files that it sees there.
  385. And there's three files that I've prepared
  386. for today's class.
  387. There's the notebook file, which is what we'll be working in.
  388. And this is the interface where we actually write the code.
  389. And then there's two data files,
  390. CSV files that we're gonna read in spreadsheets
  391. in the next few minutes
  392. to do our analysis that I've prepared.
  393. I'll tell you more about those in a minute,
  394. but that's what we're looking at.
  395. And here, as you create more notebooks or files or folders,
  396. they'll show up here.
  397. And you can make a new folder.
  398. You can import files, you can rename files,
  399. all the sort of things you might expect
  400. from a file explorer elsewhere on your computer.
  401. That's on the left.
  402. You're able to hide that by clicking the folder button,
  403. which might be nice if you have a smaller screen.
  404. I tend to leave mine open.
  405. I don't know why, just my habit.
  406. There's also a table of contents option
  407. and an outline option there,
  408. but we won't use those in class.
  409. Those are a little more,
  410. they're not essential to what we're covering today.
  411. On the top bars, of course, like any program,
  412. pull down menus that have a variety
  413. of actions available to you.
  414. We won't necessarily need to use these today,
  415. but it's the sort of things you might imagine
  416. like saving your notebook is a key one.
  417. The ability to copy and paste your code
  418. in what are called cells,
  419. which we'll get to in a minute, is very useful.
  420. And then the ability to run maybe the whole notebook
  421. in one big swoop, which we'll try later,
  422. or other things, restart it if it's having trouble.
  423. That kind of thing is up there.
  424. Then finally, there's sort of the main stage,
  425. which is where your notebook is based here
  426. on the right in the middle.
  427. And that's where you write your code and your annotations.
  428. It has a key toolbar at the top
  429. that has a number of actions
  430. that we're gonna get to in a second
  431. that allow you to create new cells,
  432. put stuff in them, run them, that kind of thing.
  433. Did I miss anything there, Ryan?
  434. No, I feel like I'm learning stuff as we go.
  435. Oh, here we got a question maybe.
  436. Hi, is it okay to ask a very quick question?
  437. Of course.
  438. I tried to save us on the computer
  439. and I'm wondering if this is a bothering for you.
  440. If so, I'll delete my saved file as soon as possible.
  441. Please feel free to save it.
  442. You know what I mean?
  443. This notebook is free and public for anyone who wants it.
  444. It's actually available on GitHub
  445. if you know where to look, you know what I mean?
  446. And once this class is over,
  447. I plan to share it on social media
  448. so as many people as possible can see.
  449. The whole point of putting it together
  450. is to share it, so feel free.
  451. Thank you, thank you.
  452. Thank you for coming.
  453. Okay, so that's the basic tour.
  454. We'll get into more details as we go, right?
  455. And the key thing in your notebook space are the cells,
  456. right, the cells are little boxes, little chunks
  457. where you can type in your Python code
  458. and then run it piece by piece
  459. as you kind of build up your notebook
  460. and build up your analysis.
  461. And right here, I've written kind of the first cell
  462. of our notebook and it has some really, really intense
  463. computer programming going on here, right?
  464. It has a math, it has math, all right?
  465. And the math is basically just two plus two,
  466. which believe it or not is Python code.
  467. Python can do all kinds of things, including math, right?
  468. And so just by typing in two plus two
  469. and writing a little Python, I've written my first code cell.
  470. I know it's a code cell because up here at the top,
  471. so I select it with my mouse
  472. and it's now blue on the left, right?
  473. That means that this cell has been selected for use
  474. by the user and you type in whatever you wanna do,
  475. we'll do one in a second.
  476. And then if you hit the play button up on the toolbar,
  477. do you guys see that, the play button?
  478. It's a little triangle.
  479. Or if you hit shift enter as a hotkey,
  480. that cell will be executed, right?
  481. And you can see that the blue bar moved down one
  482. because it's executed the cell
  483. and moved down to the next cell.
  484. And then the result of what is in the cell
  485. has been printed as an output just below it.
  486. So two plus two equals four, right?
  487. And if I were to edit that cell,
  488. if I were to say two plus two, two plus three
  489. and hit play again, it now outputs five, right?
  490. So I'm able to write the cell and run it,
  491. edit it and rerun it, right?
  492. And that's just the very basics of the notebook.
  493. I've left an empty cell just below that.
  494. And now my challenge to all of you
  495. is just to click into that cell and write a math problem.
  496. Really any problem you want, pick your favorite numbers.
  497. I don't know, just try something.
  498. All right, Ryan, what's your favorite number?
  499. What's the best number, Ryan?
  500. 2015, because that's the last time
  501. the Royals won the World Series.
  502. That definitely is it.
  503. And how many times have the Royals won the World Series?
  504. They have won it twice.
  505. So I'm gonna take it times two, right?
  506. So if I want to do 2015 and then multiplication
  507. in Python is a star or an asterisk.
  508. So if I just type 2015 times two and then hit play,
  509. I get the result, which is the next time
  510. the Royals will win the World Series in the year 4030.
  511. Yeah, that's actually accurate.
  512. Does anyone else have a math problem
  513. they'd like to share?
  514. No, okay.
  515. So that's like the very simple basics.
  516. I like the hot keys.
  517. You may see me hitting shift enter or not clicking play.
  518. I just kind of got in the habit of doing that.
  519. I'll do go there.
  520. Yeah, so the code cells aren't the only kind of cell.
  521. You may have noticed I have all this text through here
  522. that is the actual course materials.
  523. If I double click on the next cell,
  524. you'll see it turns into a box with just text in it.
  525. But if you look carefully up at the toolbar,
  526. you see it doesn't say code, it says markdown, right?
  527. Markdown is a simplified version of HTML.
  528. And so by telling Jupiter that this is not a code cell,
  529. this is a markdown cell and writing in here
  530. what I want to write, when I run it,
  531. it prints it out like text.
  532. And so stacking your code and text is the way
  533. that people kind of annotate their code and explain it
  534. and create their little scientific paper, right?
  535. And mixing those two cell types
  536. is really a big part of the art.
  537. If we go back up to my like larger box here about Jupiter
  538. with an image and hyperlinks and just click on it,
  539. you can see that it opens, it's a markdown cell.
  540. And this is how markdown does image tags.
  541. And this is how markdown does hyperlinks, right?
  542. And so I just went here before our class
  543. and typed all this out, hit play and there it is, right?
  544. All right, now not every cell runs smoothly.
  545. In fact, you'll probably before too long
  546. start having errors.
  547. I know I have them every day, all day.
  548. And when a piece of, the computer only does
  549. what you tell it to exactly what you tell it to.
  550. And oftentimes we don't give exactly the right instructions.
  551. We make a typo, we don't do something exactly right.
  552. And the computer will kind of puke out a complaint at you
  553. called an error message or a trace back.
  554. And so you can see here in this next cell,
  555. I've intentionally created an error
  556. where I've done two plus two plus
  557. and nothing else after my cell.
  558. And when I run it, I get this red box, right?
  559. And the red box is the computer, it's scary at first.
  560. You're like, what's going on, what's wrong?
  561. It's also like really kind of hard to read and odd.
  562. And it's just because Python hasn't done a very good job
  563. of making its error messages legible.
  564. But if you really stop and kind of listen
  565. to what it's trying to tell you,
  566. you can usually figure it out.
  567. And here it's a pretty simple error.
  568. It's saying there's a syntax error,
  569. which in programming means the code
  570. that you've written for me just doesn't really work.
  571. It has like something kind of fundamentally wrong
  572. about how you put the code together.
  573. And the case here is, is Python doesn't know what to add,
  574. because there's nothing after the second plus operator.
  575. It's just saying, I don't know how to do this math.
  576. There's no number on the other side of plus.
  577. And so solving that is really just as simple
  578. as sort of spotting the error,
  579. figuring out what you kind of typed wrong,
  580. correcting it and running your cell again.
  581. And you can see here just below that,
  582. I've done that with two plus two plus two,
  583. or now if I hit play on that one, it'll work.
  584. I should probably clear my outputs
  585. so that we don't have spoilers as we go down here, right?
  586. So if I had two plus two plus two, now it's six, right?
  587. Two plus two is right there, okay?
  588. Now those cells, when you do it like this,
  589. they kind of just exist independently.
  590. You know, you write some code, that's the input,
  591. you run play, it spits the output.
  592. But putting together an analysis and building something
  593. as you go down the notebook requires carrying over
  594. pieces of information between your cells.
  595. So that once something is sort of defined,
  596. it's reusable as you go.
  597. Like for instance, your data table, right?
  598. Very simple example of that is just programming variables.
  599. So that's really just like saving a piece of information
  600. in like a name so that you can reuse it
  601. over and over again as you go.
  602. And the Jupyter cells, once you define something
  603. in one cell, it's reusable in another cell, right?
  604. So here's a very simple example where I've decided
  605. to name my variable number.
  606. I could have named it anything.
  607. I could have named it Ryan.
  608. I could have, you know, named it Ben,
  609. but now I decided to name it number
  610. because that's what I intended to define it as.
  611. So right, the variable that I've called number,
  612. I'm using the assignment variable, the equal sign,
  613. to set to the number two.
  614. So if I click on this cell, which I've written
  615. and hit play, you see, we know that it's run
  616. because this little box fills in,
  617. but you'll notice there's no output, right?
  618. Unlike two plus two, there's nothing under the cell.
  619. That's because rather than just spit out its result
  620. into the notebook, we've said, hey, take the result
  621. and save it into this variable, right?
  622. And that variable is now reusable in subsequent cells.
  623. So in the next cell, I've done number plus three.
  624. So instead of two plus three, I'm doing number plus three,
  625. right?
  626. And if I play that cell, it spits out the output five,
  627. right?
  628. Because number was defined as two, it's reused here
  629. in the simple math, and then I get the output.
  630. Now, if I were to redefine number, instead of two,
  631. I want number to be three and put that in this cell,
  632. and then run the same thing again, the result is six, right?
  633. So number plus three here was five,
  634. number plus three here was six.
  635. And that's because I changed what number means, right?
  636. I redefined number as something else.
  637. And your variables can definitely change
  638. as you move down the page, that's a common technique.
  639. You gotta be careful about it though,
  640. because you might lose track of what it is as you go,
  641. or you might run your cells out of order,
  642. which could introduce some mismatches
  643. between your expectations
  644. and what code has actually been run, right?
  645. So you gotta be careful about that,
  646. but that's just part of it comes with the territory.
  647. All right, so before we move on into pandas,
  648. I just wanna challenge everybody again
  649. to try to come up with another math problem,
  650. maybe defining a variable as they go.
  651. So let's see, I'm gonna do,
  652. the last time the Chicago Cubs won the World Series.
  653. So I'm gonna say Cubs win.
  654. Notice I have an underscore,
  655. your variables can have underscores.
  656. It's gonna equal 2016, right?
  657. That's the last time the Cubs won the World Series.
  658. Now if I hit play,
  659. now that just saved it didn't go anywhere.
  660. One thing I haven't covered is how you can add new cells.
  661. So let's say I wanna use Cubs win for math
  662. and a cell just below this.
  663. If I click on the cell
  664. and then I hit this plus button in the toolbar,
  665. it's gonna add a new blank cell,
  666. which is a really common technique.
  667. You just wanna have that and save it.
  668. Okay, so Cubs win is 2016.
  669. I'm gonna do Royals win is 2015.
  670. I'm gonna run that with shift enter.
  671. I'm gonna add another cell.
  672. And then I'm gonna ask a really clear question
  673. that is science, science.
  674. Is Cubs win greater than Royals win, right?
  675. Are the Cubs better than the Royals?
  676. Let's get the answer mathematically, right?
  677. Greater than is another operator.
  678. There's a less than.
  679. And the answer is of course true.
  680. The Cubs are better than the Royals.
  681. Did you know that, Ryan?
  682. I'm sad to admit that I did know that, man.
  683. All right.
  684. Was everybody able to experiment with their cells,
  685. maybe make a variable, do a little math?
  686. Any errors encountered?
  687. I always love hearing about errors.
  688. I collect them like a zoologist.
  689. Okay, you guys are sharp.
  690. You're sharp or you're bored with me.
  691. I'm gonna keep rolling.
  692. We're gonna get into the exciting stuff soon.
  693. Our next section is introducing pandas.
  694. I've already explained it.
  695. Pandas is really a fancy spreadsheet
  696. that you're running Python that can do all kinds of stuff.
  697. And we're gonna get into what that stuff is pretty quick.
  698. But to use it, you have to import it.
  699. Pandas isn't available.
  700. The plus sign, the minus sign, those are just there.
  701. Python has them.
  702. They're in the standard library.
  703. When you install Python, you got that stuff.
  704. But a lot of people used Python
  705. to create other Python tools,
  706. which they then give away to the world for free,
  707. but you have to install them before you can use them, right?
  708. And they're in something called the Python package index,
  709. which you can read more about if you link.
  710. And one that's used by lots and lots of people is pandas.
  711. And once you have pandas installed,
  712. which Jupyter Lite, gratefully,
  713. we're grateful for, does for us, right?
  714. You need to import it.
  715. So anytime there's a Python library
  716. you wanna bring into your notebook for use,
  717. you have to use this little import command that's built in
  718. and then the name of the package.
  719. And if all these packages have websites or they're on GitHub
  720. and you can usually see there in some documentation
  721. or goof around a little bit
  722. and figure out what the right thing to import is.
  723. In the case of pandas, it's import pandas.
  724. So I want everybody to click on this cell and hit play.
  725. Boom, we can see nothing happened
  726. and the number filled in here on the left.
  727. That's good news.
  728. That means our browser is able to bring in pandas.
  729. It's available.
  730. It's already installed.
  731. If you try to import a package
  732. that isn't installed on your computer,
  733. you're gonna get an import error, which is really common.
  734. It just means you gotta get the tool installed,
  735. which usually means using something called pip
  736. or other Python tools that are capable of downloading
  737. and putting extra Python packages on your computer, right?
  738. Hey, Ben, what does it mean when a little asterisk
  739. shows up inside the cell number next to the cell I'm running?
  740. Great question.
  741. So when I hit play here, keep your eye where it says 17
  742. and you'll see, it went to 18.
  743. Okay, well, between 17 and 18,
  744. there's often a little asterisk that you'll see pop in
  745. and that means the computer is working, right?
  746. So sometimes your computer code takes more than a second
  747. to like finish and while you're waiting
  748. for the computer to finish, inside that box,
  749. you're gonna see an asterisk.
  750. In the upper right-hand corner where it says Python up here
  751. and there's this little empty circle,
  752. that will also fill in
  753. and it just means your computer's working, right?
  754. And since pandas is a lot of code, it's a big library,
  755. sometimes it can take a while to import it,
  756. especially if you're running it in a lightweight tool
  757. like Jupyter Lite.
  758. So don't stress, just be patient, right?
  759. And let it finish.
  760. Those are moments of drama though, will it work?
  761. You know, the tension rises.
  762. Okay, so we have in pandas imported
  763. and now just to demonstrate like you don't have to do this,
  764. I'll make another cell and now pandas the variable,
  765. if I hit, if I type pandas, do a dot,
  766. which we'll learn more about later and hit tab,
  767. we'll see this auto complete opens
  768. and this is Jupyter showing us
  769. the hundreds of utilities on the pandas tool belt.
  770. So the pandas tool belt can do lots
  771. and lots of different things
  772. and they're all available via this pandas variable
  773. that we're gonna use later
  774. to sort of take them off the tool belt
  775. and put them to use, right?
  776. I wanna delete this cell,
  777. like one handy way to delete it is just to cut it.
  778. So I'll click right there and I'll hit the scissors,
  779. boom, the cell's gone, right?
  780. You can also hit X on your hotkeys
  781. and that will delete the cell as well after.
  782. Hey Ben, I think Catherine maybe has a question.
  783. Yeah, let's hear it.
  784. No, no, I'm okay.
  785. Oh, awesome, right?
  786. I'm just here, okay, thank you.
  787. Love it.
  788. So now this next cell, I'm gonna import pandas,
  789. but I'm gonna do it a different way.
  790. If you've ever seen pandas code before,
  791. you've probably noticed that the variable PD
  792. is all over the place.
  793. So it's PD this and PD.that, not pandas.
  794. And all that is is a shortcut, right?
  795. So the people who invented pandas,
  796. even though they decided to call it pandas,
  797. they felt it was too long
  798. and it was just a hassle to have to type pandas all the time.
  799. They wanted to make their code more compact
  800. and be able to write it faster.
  801. And so they introduced kind of a,
  802. a really a style convention, you know,
  803. which is abbreviating pandas as PD.
  804. So when you import a library in Python,
  805. you have to have the full name of it here,
  806. but if you put as and then a new variable after as,
  807. you can redefine it as an alias or something shorter.
  808. And so in almost all of pandas documentation
  809. and examples you'll find in Google searches,
  810. you're gonna see PD used, right?
  811. And that's because they've done an import in this style.
  812. You're technically not required to do that.
  813. It's entirely optional.
  814. It's just sort of a matter of art, right?
  815. And we're gonna do that in this class
  816. just to kind of fit in, you know, peer pressure.
  817. Okay.
  818. And so to do that, I'm gonna click on this cell
  819. and I'm gonna hit play.
  820. And you're gonna need to do that too,
  821. because this PD variable is gonna be really important to us
  822. as we go down the page,
  823. cause we're gonna be using it again and again to do things.
  824. So go ahead and run that cell and hit play.
  825. Once we've done that, we have our tool belt
  826. and we're ready to start working with math
  827. and doing stuff with pandas.
  828. Before we get into the data from the story,
  829. like the real world stuff,
  830. I wanna use a simplified dataset
  831. just to demonstrate some of the principles of pandas
  832. in a way that's really easy to keep track of
  833. and kind of introduces you to some of its functionality,
  834. right, before we get into something more complicated.
  835. And that's just gonna be calculating
  836. basic descriptive statistics.
  837. One thing pandas is really good at
  838. is taking a list of numbers and doing stuff with them,
  839. not just adding them or subtracting them,
  840. but calculating all the statistics
  841. that we use to do stories like mean and median
  842. or min and max or all those kinds of things.
  843. And you're able to do that in Python.
  844. Python can do those stats,
  845. but you often have to write quite a bit of code to do it,
  846. or you have to be kind of good at Python to figure it out.
  847. You have to write the math formula, right?
  848. Well, pandas has all the math formulas
  849. already worked out for you,
  850. so you don't even have to know them.
  851. And it could do them with a very small amount of code
  852. and it could do it really quickly over gigantic datasets,
  853. which is why people like it, right?
  854. So a simple example just to start with
  855. is here this next cell
  856. where I've created a sort of example dataset.
  857. It is the even numbers between zero and 10, right?
  858. And so here I have a variable, I've called it my list,
  859. just cause like I couldn't think up a more creative name,
  860. right?
  861. We'll say my list, right?
  862. And we're gonna say equals.
  863. So the variable will be equal to,
  864. and here's something we haven't seen yet.
  865. This is just a list.
  866. So Python has a data type called a list,
  867. which is kind of like a column and a spreadsheet,
  868. if you think about it, right?
  869. And it has all of the values between two flat brackets
  870. and everything in that flat bracket
  871. is in a list that you can then run
  872. sort of special operations against.
  873. And so we're gonna start with this very simple list
  874. that I've pre-written two, four, six, and eight.
  875. Hit play.
  876. My list has now been saved to equal that.
  877. If I were to add a new cell
  878. and I were to just run my list,
  879. you would see that there in the output,
  880. it spits out the result, right?
  881. So we took our list, we saved it as the variable.
  882. If I just run the variable in another cell,
  883. it spits it out, it's there, right?
  884. For instance, if I were to change this to seven,
  885. rerun it again, two, four, six, seven, right?
  886. I think you guys are getting the hang of it, I bet.
  887. Okay, great.
  888. So that's the standard Python list.
  889. That's the vanilla list.
  890. We wanna upgrade this list to be a pandas list
  891. so we can take advantage
  892. of all pandas grade statistical functions.
  893. Panda's term for a list is a series.
  894. And again, that's just a term of art
  895. that the pandas developers came up with
  896. for a list of things, right?
  897. They couldn't call it a list
  898. because Python already calls it a list.
  899. So they had to call it something else.
  900. They decided to call it a series, okay?
  901. And you can see here in this next cell
  902. that I begin with the PD variable, which is pandas, right?
  903. A period, which is how we begin to access
  904. the many different tools on the pandas tool belt.
  905. And then the name of the tool I want.
  906. The name of the tool I want is capital S series.
  907. I know this because it's in the pandas documentation
  908. and I read about it elsewhere.
  909. There's hundreds of other things in pandas
  910. I know nothing about, right?
  911. We only need to know five or 10 of these pandas terms
  912. to like do stuff in this class,
  913. but you will kind of gradually have to learn them, right?
  914. And so here I'm saying so pandas,
  915. I want you to make a new series.
  916. I wanted to find a series, capital S.
  917. And then there's an open parenthesis and a close parenthesis,
  918. which is a common computer programming way
  919. of calling a function or of kick-starting a process,
  920. which requires an input,
  921. something to be, you know, submitted to be created.
  922. And so here I'm saying pandas,
  923. I want you to make a new series
  924. and the input into that series is my list variable
  925. of two, four, six, and eight.
  926. And the result of that, I'd like you to name my series,
  927. right?
  928. So we're gonna make a new variable based on the list
  929. that will be the series.
  930. And so I'm gonna run that cell
  931. just to get into demonstrate what's going on.
  932. I'm gonna, you don't have to do this.
  933. I'm gonna make another cell and spit it out.
  934. And we can see here, yes,
  935. it no longer prints a little list.
  936. It now has this sort of other like ugly thing,
  937. which is pandas sort of representation
  938. of what a series is, right?
  939. A series is a list of things.
  940. There they are two, four, six, and eight.
  941. And then pandas has sort of what they call an index
  942. or, you know, just like an incrementer on the left
  943. that counts the rows.
  944. So row zero, computers like to start with zero
  945. when they count.
  946. No one's ever explained that to me.
  947. The first row, row zero is two.
  948. The second row, row one is four, et cetera, et cetera, right?
  949. So we now have a series.
  950. And now that our list is a series,
  951. it has the superpowers of pandas, right?
  952. So in the same way that we could do PD dot
  953. and access all kinds of stuff, my series dot,
  954. if I hit tab, I get the auto complete, right?
  955. We can see your series has dozens and dozens
  956. of special things it can do, right?
  957. And again, you only probably ever need to learn
  958. a couple of these, but they are just different
  959. math operations, different stuff.
  960. And we're gonna go through a few as we continue.
  961. And some really valuable ones are the runs
  962. that do descriptive statistics.
  963. So in that long list were things that calculate,
  964. say the sum, right?
  965. So my series dot sum, if I hit play,
  966. there's the open parenthesis, close parenthesis,
  967. which is again, sort of calling something,
  968. issuing a command for the computer to do something for you.
  969. But there's no input in this case.
  970. And the input isn't needed
  971. because the data is already in the variable, right?
  972. So Python doesn't need you to give it the list
  973. to do the sum, it already has it in the variable.
  974. And so just by naming your series
  975. and running dot sum, open parent, close parent,
  976. boom, it spits out the answer 20, right?
  977. And if we go do the math ourselves too,
  978. plus four is six, plus six is 12, plus eight is 20,
  979. that checks out, right?
  980. So we've been able to total that right there.
  981. You can also do the maximum value,
  982. my series dot max, right, is eight,
  983. that's the highest value, my series dot min
  984. is of course two, mean is the average, right?
  985. You can really quickly calculate the average.
  986. The median, which is not fun to calculate on your own,
  987. right, you can just boom right there,
  988. get the median is also five.
  989. The unfortunately named STD function
  990. does the standard deviation.
  991. And then there's one sort of mother of all functions
  992. called describe that will just take your series
  993. and it'll run a whole set of descriptive statistics
  994. for you automatically,
  995. tell you how many items there are in the list
  996. for the average standard deviation min max.
  997. And all you statistical folks on this call know
  998. that the 50th percentile is of course the median, right?
  999. Right there in the middle,
  1000. get a couple other cuts too.
  1001. And this is pretty simple math, of course, or whatever,
  1002. but it's important to keep in mind the principle
  1003. that if my list contained one number
  1004. or if my list contained 1 million numbers,
  1005. the computer programming code that you wrote
  1006. would be exactly the same, right?
  1007. You're able to run some of these stats
  1008. and without any more code than you just saw right here
  1009. in this notebook.
  1010. And it's pretty easy, right?
  1011. And just to demonstrate a little bit of the notebook
  1012. and how that's possible,
  1013. if I were to go in here and change this to the odd numbers,
  1014. one, three, five, and seven,
  1015. and then I were just to go quickly
  1016. and just run all my cells, boom, boom, boom, boom, boom.
  1017. We can see that all of the outputs have modified
  1018. and changed to reflect that we're now running
  1019. the same statistics on a different series of odd numbers,
  1020. right?
  1021. So Ben, I know that you're using this as an opportunity
  1022. just to kind of introduce some of what panels can do.
  1023. To me, it looks like describe and median
  1024. and some of these functions
  1025. that you're walking us through
  1026. are pretty useful in exploring a dataset
  1027. that you don't know anything about,
  1028. kind of coming into a project
  1029. where you don't know what the outcome is gonna be.
  1030. And it seems like the notebook,
  1031. I'm putting this on a tee for you, Ben.
  1032. Do you use notebooks to just kind of explore things
  1033. before you know what you're doing
  1034. like we do in our class today?
  1035. Yeah, that's usually how it starts for me
  1036. in a journalism project,
  1037. is I have a dataset that,
  1038. I've been seeking out that I want,
  1039. but I don't know exactly what's in it.
  1040. And I'm often using the notebook
  1041. to kind of ask questions of the data.
  1042. How many rows are there in this darn thing?
  1043. What are all the columns?
  1044. We'll get to that in a bit.
  1045. What's the distribution of the values?
  1046. And just exploring the data is definitely where I think
  1047. almost all a data journalism project start
  1048. unless you really have already mastered the dataset
  1049. and fully understand everything about it,
  1050. which is for me is never.
  1051. That's usually where it begins.
  1052. And my process from like beginning exploration
  1053. to a published story usually involves
  1054. like at least three drafts of the notebook,
  1055. with the first one being full of all kinds of doodles
  1056. and questions and like mess
  1057. as I try to figure out what's in there,
  1058. ultimately honing that down to the findings
  1059. that I want to like share with the audience.
  1060. And the final version,
  1061. I usually limit to just sort of the findings
  1062. and the shortest line from the data to the end
  1063. just to sort of spare the reader.
  1064. You know what I mean?
  1065. Of all my missteps and sort of things.
  1066. So that is to say, when you first begin,
  1067. it is totally normal for your notebook to be a mess.
  1068. You know what I mean?
  1069. As you kind of sort out what the heck you're doing,
  1070. but I really would encourage you to go back to it
  1071. and revise it and work over it.
  1072. And to be honest with you,
  1073. that's really the power of this
  1074. is that you're able to like incrementally strengthen
  1075. your analysis and your process by returning to it,
  1076. editing it and going back over and over again.
  1077. Where if you're just always exploring
  1078. or you're goofing around in a spreadsheet,
  1079. it can be tough to like, you know,
  1080. put one foot in front of the other
  1081. and really get stronger and stronger as you go.
  1082. That's, I'll just throw in one of the things
  1083. that I think is so cool about notebooks like this is
  1084. you're just, you're documenting that exploration as you go.
  1085. Like it's, I find it so annoying to like,
  1086. all right, I gotta take time from the thing
  1087. that I'm working on to go like write my notes,
  1088. but like this is just kind of doing that automatically
  1089. as you go, you're kind of keeping notes
  1090. that you can throw a markdown cell in there
  1091. to kind of give yourself some text notes,
  1092. but you're seeing the process that you use
  1093. to get where you ended up.
  1094. And like being able to reproduce your process
  1095. is super helpful when it comes time
  1096. to double check your work, like all those things.
  1097. So working in a notebook,
  1098. you're just kind of getting all those notes for free
  1099. in a way that you don't necessarily have
  1100. if you're just writing code in another system.
  1101. Totally, that's why I love it.
  1102. So that's how series works,
  1103. which you can also think of as columns
  1104. and some of the basic stats.
  1105. Do we have any questions about that
  1106. before we move on to tables and some real data?
  1107. Excuse me, speaking about averages,
  1108. how can I get the mode of the series?
  1109. How can you get the mode?
  1110. That's a great question, I didn't cover that.
  1111. And it's not in the describe function.
  1112. So it's just something they maybe overlooked
  1113. or didn't include for some reason when they created this.
  1114. Just to kind of demonstrate the process,
  1115. I have questions like that all the time.
  1116. How do I get pandas to do this?
  1117. How do I get pandas to do that?
  1118. You know what I mean?
  1119. You're gonna be in that position all the time
  1120. when you're doing that.
  1121. And the number one way, it seems like simplistic
  1122. not to like whatever be whatever about it
  1123. overly simplistic, but Googling it
  1124. is like the way to almost always do it.
  1125. So like, honestly, like figuring out
  1126. how to write the Google searches
  1127. that get you where you wanna go with pandas is part of it.
  1128. And it's not that hard.
  1129. You just kind of have to struggle
  1130. to get what you're looking for.
  1131. So like, to me it would be like,
  1132. get mode or you know, mode of pandas column.
  1133. See right there, get mode of pandas column.
  1134. It's like, you know, one already there.
  1135. And you can see the first result
  1136. is actually the documentation for pandas itself, right?
  1137. Which is not always the easiest to read
  1138. but can be pretty helpful.
  1139. And we can see here that it's saying
  1140. that there's a dot mode, right?
  1141. Here it's saying the mode is on the data frame
  1142. which we haven't got to yet
  1143. but I bet you it's gonna work, right?
  1144. And so if I go back here, I add another cell.
  1145. I'm gonna do my underscore series dot mode.
  1146. I'm experimenting.
  1147. I'm not even sure, right?
  1148. I hit play.
  1149. That doesn't look like it's it, does it?
  1150. 1, 3, 5, 7, no, that didn't work.
  1151. That's exactly what I tried.
  1152. I Googled it in the, tried that method
  1153. and I give that same result.
  1154. Yeah, so like, you know,
  1155. I honestly don't know the answer to this off my head.
  1156. So let's struggle through it together.
  1157. Another website you'll commonly run into is Stack Overflow
  1158. which is where people go with their questions like this
  1159. and then other users like help them.
  1160. And so this guy is saying there's a data frame dot mode.
  1161. This person is, and that's not what we want.
  1162. We tried that, right?
  1163. This person is saying,
  1164. oh, I import a different library to calculate the mode.
  1165. I'm using a different Python thing, right?
  1166. We actually don't have a mode in this dataset though, right?
  1167. Cause there's no duplicate numbers, right?
  1168. Cause they're all there equally.
  1169. Let's add a number that's there more than once, right?
  1170. The mode is the most common number in the dataset
  1171. in terms of frequency for folks who may not know.
  1172. So I'm going to add the number one in there
  1173. like a bunch of times, right?
  1174. Yes, you're right, yeah, you're right, right.
  1175. And then I update the series.
  1176. So I updated my list and I reran the series
  1177. and then I go down and you can often do this in your notebook
  1178. just run the cells you're most interested in.
  1179. Ah, now that worked, right?
  1180. Once we had introduced that there was more than,
  1181. there wasn't a tie, right?
  1182. We kind of created a winner of the mode contest.
  1183. Then the number one came up first.
  1184. So now for instance,
  1185. if I go and change these all to twos, right?
  1186. I rerun my list.
  1187. I have to rerun my series to update the series, right?
  1188. And then I rerun the mode.
  1189. Now it's two, right?
  1190. Thank you, thank you.
  1191. I understand.
  1192. Yes, yes, I tried the same thing.
  1193. No, no, thank you.
  1194. Excellent, and that process you just saw.
  1195. So the method is kind enough to show all the tie,
  1196. all the tie items, not arbitrary one,
  1197. but if there's any ties, the method shows all the ties.
  1198. It does appear to do that.
  1199. I didn't know that, I just learned that, you know?
  1200. But that does seem to be true.
  1201. And like that process that you just saw me stumble through,
  1202. how do I do this thing?
  1203. Google it.
  1204. I think I know what this is telling me,
  1205. but I really don't, right?
  1206. I don't want to import another library, that seems dumb.
  1207. What?
  1208. And then ultimately it came back to me thinking like,
  1209. oh, my dataset doesn't really have a mode, right?
  1210. And then I've got to tweak something,
  1211. only rerun two or three cells,
  1212. and then I get it like that cycle of like,
  1213. I have a question, I'm confused, I experiment,
  1214. I find something I didn't expect, a bug or everything.
  1215. And then I target, I update just the cells I need to.
  1216. That is the cycle.
  1217. That's it right there, guys.
  1218. You know, it was like a really simple example of it.
  1219. So thank you for sharing it.
  1220. But you're going to be doing that all the time
  1221. as you do this work.
  1222. You know, just solve different types of problems.
  1223. Thank you.
  1224. Well, I can see the good point
  1225. of the reproducible notebook, thanks.
  1226. Exactly.
  1227. Because if you were just writing Python code in a text file,
  1228. you'd have to rerun all the code from the start.
  1229. But here we were able to just, in a targeted way,
  1230. rerun just the cells we needed to, right?
  1231. Which, you know, sometimes cells take a long time to run,
  1232. that could be really useful to not have to rerun at all.
  1233. Yeah.
  1234. Okay, great.
  1235. Thank you so much.
  1236. So we're going to move into our next section,
  1237. which we're going to talk about data frames.
  1238. And just like series is sort of like
  1239. the pandas brand name for a list.
  1240. Data frame is just like the brand name for a table
  1241. or a spreadsheet, right?
  1242. It actually isn't invented by pandas.
  1243. Data frame is like a more general computer programming term.
  1244. I don't actually know the origin.
  1245. If anybody knows the origin, I would love to hear it.
  1246. But all it really is,
  1247. it's just another name for a spreadsheet,
  1248. except it's one that computer programming languages
  1249. can muck around with, right?
  1250. So if you ever get introduced to the R programming language,
  1251. which is used for a lot of statistics,
  1252. they call tables data frames too, right?
  1253. And I think there's probably other libraries
  1254. that call them other things.
  1255. And it's just what people have decided to call a table.
  1256. And what is, of course, a table?
  1257. It's a collection of columns and rows.
  1258. Instead of just like one series or list,
  1259. it's a bunch of them, you know, side by side.
  1260. And then they tend to have a name at the top
  1261. that tells you what is in that column,
  1262. which is often called the header, right?
  1263. And so we're gonna use two tables,
  1264. two sets of data in this class.
  1265. And we're gonna get into the real world stuff here.
  1266. So we're using two manicured kind of cleaned up data sets
  1267. from this story that I worked on that published in 2018,
  1268. which is about the Robinson R44 helicopter.
  1269. It's an investigation I did with Kim Christensen
  1270. at the LA Times.
  1271. And our finding based on the data analysis
  1272. was that the R44,
  1273. the world's best selling civilian helicopter,
  1274. is actually also has the highest accident rate
  1275. of any helicopter.
  1276. So, you know, is that the fault of the pilots?
  1277. Is that the fault of the helicopter?
  1278. The storage gets into all that, right?
  1279. But the statistics are, and it's disputed.
  1280. The statistics are undisputed.
  1281. The Robinson helicopter has the highest accident rate.
  1282. And the data that is in this notebook
  1283. that we're gonna go through is how I established that
  1284. so that we could report it in the story, right?
  1285. And the way I did that, I calculated the rate
  1286. is on the one hand,
  1287. I had the database of all helicopter accidents,
  1288. which came from the National Transportation Safety Board.
  1289. We can see that right here in the left in the finder,
  1290. that's our NTSB accidents,
  1291. National Transportation Safety Board accidents.
  1292. If I double click on it over there on the left,
  1293. look at that, in a new tab here,
  1294. Jupiter opens the spreadsheet, you know,
  1295. you can't really edit it, but you could preview it.
  1296. And we see here in this spreadsheet,
  1297. this is the data that I've kind of put together for class.
  1298. And, you know, one thing I always ask myself
  1299. when I'm inspecting data is what is a row?
  1300. What is a row equal?
  1301. And here, if we look at this,
  1302. we can see that each row is a helicopter accident, right?
  1303. And I've put into, and this is the unique idea of the event.
  1304. And then we have here the make and model
  1305. of the helicopter from the NTSB, the year of the accident,
  1306. the date of the accident, the location,
  1307. how many people died.
  1308. And then we have here this LA times make
  1309. and LA times model, which we'll get to,
  1310. which is the standardized cleaned up version
  1311. of the government data,
  1312. which had a lot of variations and noise in it.
  1313. And we'll get to it as we go, right?
  1314. And so this is the columns in the data.
  1315. And so in Panda's terminology, this is a data frame,
  1316. and each column is a series, right?
  1317. And so that's dataset one,
  1318. which has all the helicopter accidents.
  1319. Dataset two is from the Federal Aviation Administration,
  1320. and it's survey data that tells us
  1321. how many total flight hours each
  1322. of the major helicopter models had
  1323. over the period of time that we're studying.
  1324. And this is data that is actually derived
  1325. from a long and winding FOIA request
  1326. that I had with the agency, we won't get into,
  1327. that I've tidied and cleaned up here
  1328. to try to make this class a little more like a cooking show
  1329. and to move along a little quicker.
  1330. But basically we can see here that each row
  1331. is an LA times make and model,
  1332. a cleaned up helicopter, right?
  1333. There's the Robinson R44 right there in row 10.
  1334. And on the right is the total number of hours
  1335. based on survey data that it's estimated
  1336. that helicopter was in the air,
  1337. that pilots were flying that helicopter during the period.
  1338. And if you think about it,
  1339. you're trying to adjust, right?
  1340. The accidents versus how often then they're in the air
  1341. to try to make fair comparisons for different helicopters.
  1342. And this is one technique for doing that.
  1343. And so as we go, we're gonna combine these two data sets
  1344. and calculate that rate using data frames, right?
  1345. Any questions about the data sets
  1346. before we get into how to use them with pandas?
  1347. No, all right, let's keep going.
  1348. So in our next cell, we have here our friend PD again.
  1349. PD just as a reminder is the shortcut for pandas.
  1350. So PD dot is what gives us access to all of pandas,
  1351. many, many, many tools.
  1352. One of those tools that you'll use a lot is read underscore CSV.
  1353. This will read in a CSV file.
  1354. A CSV file is a file of comma separated values.
  1355. It's a spreadsheet, right?
  1356. It's, you know, you've all seen them, right?
  1357. And so a CSV file, we just opened them and looked at them.
  1358. A CSV file can be parsed and read in by pandas
  1359. as a data frame using the read CSV function.
  1360. Again, the parenthesis to sort of call it.
  1361. And then it wants you to give the name or path to the file
  1362. inside of quotes, so it knows where to find it.
  1363. That's the input in this case.
  1364. So I do pandas dot read underscore CSV.
  1365. And I've typed in the name of the file,
  1366. which matches what's in our finder over here.
  1367. And I hit play.
  1368. And boom, you can see right there below it,
  1369. it spits out the result of that function.
  1370. And the result is a table that looks a little bit
  1371. like that spreadsheet we were just looking at, right?
  1372. This is the sort of pandas representation of the data frame.
  1373. The notebooks puts it out in a way
  1374. that we can kind of scan it, look around at it.
  1375. And this is what's also what's nice about the notebook
  1376. is you can inspect your things as you move along
  1377. to kind of look at them
  1378. without having to rerun all your code, you know?
  1379. And so we could see here, there's the table
  1380. as we saw in the preview, but now it's in Python.
  1381. It's been read in by pandas, but it's just being spit out.
  1382. So we can't reuse it.
  1383. In our next cell, we're gonna save it
  1384. so that we can use it in subsequent cells
  1385. and start to do analysis.
  1386. And it's the same thing as we did with those numbers earlier
  1387. on, exact same concept.
  1388. We name a variable.
  1389. We can name it whatever we want.
  1390. I'm doing accident underscore list, right?
  1391. Then equal to define it.
  1392. And that exact same code from above.
  1393. So the same code from up here with read CSV is down here.
  1394. It's now being saved to a variable.
  1395. If I run play, nothing happens.
  1396. The number shows up.
  1397. That means the data frame has been saved
  1398. in the same way that we saved our series, right?
  1399. And in the same way that we ran statistics on our series,
  1400. we can do all sorts of things with our data frame, right?
  1401. So again, just to make a new cell,
  1402. if I type in accident underscore list, hit dot,
  1403. and then tab, we get the preview.
  1404. And there are the many, many, many, many, many, many things
  1405. a data frame can do, right?
  1406. You'll never learn most of them.
  1407. It's fine.
  1408. But there's a few that you're gonna use all the time.
  1409. And one is called head.
  1410. So your data frame variable, whatever you might call it,
  1411. dot head, open parenthesis, close parenthesis,
  1412. will print the first five rows of your data frame.
  1413. And that's just a really easy way to get a look at it
  1414. and see what's in there.
  1415. Take a sniff, right?
  1416. Head does the top of the data frame.
  1417. There's another one called tail.
  1418. That'll give you the bottom of the data frame.
  1419. Ha ha.
  1420. Head and tail, right?
  1421. It's five by default, right?
  1422. But if you were to pass a number into head as an input,
  1423. you would override that default
  1424. and you would tell the computer,
  1425. no, I don't want five.
  1426. I just need one.
  1427. Or maybe I want 100, right?
  1428. It's really up to you.
  1429. So head is really useful.
  1430. I would encourage you to run it on every data frame
  1431. that you work on.
  1432. Another one that I would encourage you to always run
  1433. is dot info.
  1434. Dot info, like dot describe, prints a little report card.
  1435. And so we have here a report card about what's in our table.
  1436. It's gonna be really helpful.
  1437. It tells you how many rows there are.
  1438. It's 163 rows, right?
  1439. So 163 helicopter accidents, right, in our dataset.
  1440. And then we have here a list of our columns,
  1441. which were the same ones we previewed,
  1442. but we have, it's all spelled out
  1443. so we can refer back to it later if we need to, right?
  1444. And then like any good database or data frame,
  1445. every column has a type, a data type,
  1446. which tells the computer
  1447. what type of things they can do with it.
  1448. Like if you wanna do math, right,
  1449. you have to have numbers.
  1450. And numbers, right, are integers,
  1451. is another term for number.
  1452. And you can see here that this column, aircraft ID,
  1453. this column, total fatalities,
  1454. float is another term for numbers.
  1455. That's a number with a decimal, right?
  1456. You can see that pandas has guessed the data type
  1457. of a lot of our different columns,
  1458. which is here on the right.
  1459. And it's just not very human readable
  1460. because the pandas people have done a bad job.
  1461. But basically it's telling you
  1462. that the computer has guessed
  1463. that these are integer fields,
  1464. which are numbers without decimals.
  1465. These are floating fields,
  1466. which are numbers with decimals.
  1467. And so these columns could do math.
  1468. They could do .sum.
  1469. They could do .min.max, right?
  1470. The other columns pandas says are objects.
  1471. This is another poorly named thing,
  1472. but objects just means it's characters or strings.
  1473. It's words, right?
  1474. Like the name of the helicopter, right?
  1475. Or the name of the state where something is based.
  1476. And words can't do math, right?
  1477. And that's because,
  1478. and the data type of the computer kind of regulates that.
  1479. And so it's very important to make sure
  1480. that your columns have the data types
  1481. that you expect or that you want.
  1482. A really nice thing about pandas
  1483. is it guesses them for you and it's almost always right,
  1484. which is less work for you, right?
  1485. You're really just reviewing the computer's work.
  1486. But sometimes it's wrong
  1487. and you need to override that.
  1488. And there's ways to do that with read CSV
  1489. that we won't cover here.
  1490. Was there a question?
  1491. No, okay.
  1492. So that is the introduction to the data frame, right?
  1493. And we know from looking at it,
  1494. we've made our first conclusion, our first finding.
  1495. There were 163 fatal accidents, right?
  1496. And I know that because that's how many records
  1497. there are in the table.
  1498. Analysis.
  1499. Now to work with your data frame,
  1500. you often need to access your columns, right?
  1501. So there's all these different columns in the data frame.
  1502. How do you get your hands on them, right?
  1503. And we saw here in the info,
  1504. it printed out their names right there, right?
  1505. And there's this one we've seen a couple of times,
  1506. LA Times Make and Model.
  1507. So what kind of helicopter it was
  1508. after Ben has cleaned it up
  1509. and fixed the government's data, right?
  1510. So that's the column that will have the name
  1511. of the helicopter in each accident.
  1512. And because its name is LA Times Makes and Model,
  1513. if you do accident list dot
  1514. followed by any of your column names,
  1515. it's gonna spit them out, right?
  1516. So there's that column, LA Times Make and Model,
  1517. spit out as guess what, a series, right?
  1518. And if I were to go up here
  1519. and I were to take state instead,
  1520. and I were just to paste state over it and run it again,
  1521. there's all the states, right?
  1522. And so any column that is in your data frame
  1523. is accessible via the dot,
  1524. which is called dot notation and they're there.
  1525. Now, if your column name has spaces in it,
  1526. which is pretty common, the dot won't work
  1527. because the computer won't know how to read the code.
  1528. It'll think that you're writing two different things
  1529. and it'll throw an error like that.
  1530. But don't worry, there's a way around it.
  1531. If instead you do a flat bracket,
  1532. flat bracket like that,
  1533. the exact same thing happens.
  1534. And if your column had a space in it,
  1535. it would work fine, right?
  1536. I like to use the dot
  1537. because it's just easier to read, it's less code.
  1538. That's why I often give my headers underscores
  1539. rather than spaces.
  1540. But that's really just a matter of personal preference.
  1541. It's not right or wrong.
  1542. Shouldn't put you like it, don't like.
  1543. Some people don't wanna deal
  1544. with having to do two different ways.
  1545. And so they always do it with the flat bracket,
  1546. which is also fine, you know?
  1547. Now, that series has its own, you know, dot tab
  1548. where we can see the many, many things we can do with it.
  1549. And because this column is not an integer or a float,
  1550. it's a string or object column,
  1551. we can't do min and max because it's not math.
  1552. They're not numbers, they're words, they're strings, right?
  1553. But they have their own set of very interesting
  1554. dot things that numbers don't have.
  1555. One that's super helpful and we use all the time
  1556. is called value underscore counts.
  1557. So if you do the name of your data frame,
  1558. a dot, the name of your string column, a dot,
  1559. and then this fun function value counts,
  1560. open print, close print, and hit play,
  1561. boom, you get printed out a group and count on that column,
  1562. a frequency table or what Excel calls a pivot table
  1563. instantly, wasn't that easy?
  1564. And we see here that of our, what was the total again?
  1565. Of our 163 helicopter accidents,
  1566. 38 were the Robinson, R44, right?
  1567. 30 were the Bell 206.
  1568. We have an accident ranking just like that, right?
  1569. So just with that one little line of code,
  1570. we've answered an interesting question,
  1571. which helicopter make and model
  1572. had the most helicopter accidents?
  1573. Congratulations, right?
  1574. Now you might see the series,
  1575. this doesn't look like a data frame.
  1576. It doesn't have like the nice clean column headers
  1577. and rows and things.
  1578. And that's because this is technically a series
  1579. that has some numbers annotate on it in pandas world.
  1580. If you wanna tidy this up and make it look like a data frame
  1581. a little more legible, there's this weird pandas function
  1582. that you'll have to use from time to time
  1583. to do that called reset index.
  1584. What that really does is it takes the thing that you have
  1585. and it converts it into a data frame.
  1586. Why is it called reset index?
  1587. I don't know, it's a bad decision.
  1588. It's what the pandas nerds did.
  1589. And one thing about working with open source software
  1590. is you're just kind of stuck oftentimes
  1591. with how things were designed by the people who designed it.
  1592. I mean, it is free after all
  1593. and who are we to complain?
  1594. And so I think just as a beginner,
  1595. it's good to just adapt, learn the quirks, live with them.
  1596. So I hit play there.
  1597. You can see it's now a nice legible data frame.
  1598. If you get more advanced at Python
  1599. and these things irritate you, it's all open source.
  1600. Everything's on GitHub.
  1601. You can go and suggest a change to the people who run it
  1602. and maybe they'll listen.
  1603. I've tried on this one, they didn't listen.
  1604. All right, so now we're gonna do a little bit
  1605. of an exercise together using only techniques
  1606. we've learned here in class,
  1607. nothing that hasn't been covered.
  1608. And we're gonna answer some journalistic questions
  1609. in the cells below.
  1610. It's gonna be fun, it's gonna be a little challenging.
  1611. And then once we finish that,
  1612. I think we might take a short break, okay?
  1613. Just to give everybody a minute
  1614. and to give me a chance to get some water.
  1615. All right, so here we go.
  1616. I'm gonna need some help from the crowd, guys.
  1617. Here's the questions from your annoying editor, right?
  1618. Okay, so I know how many accidents there were, right?
  1619. Hey, there were 163 accidents.
  1620. But how many people died?
  1621. Because more than one person can die
  1622. in a helicopter crash, right?
  1623. How using only the data we have
  1624. and only techniques we've learned so far,
  1625. could we answer the question of what was the total number
  1626. of fatalities in the 163 accidents?
  1627. It's gonna require a little creativity, a little stretch.
  1628. Anybody have a thought?
  1629. Even if you don't know how to write the code,
  1630. you have an idea of how we might calculate it from the data.
  1631. Well, we have to sum the total fatalities column.
  1632. You got it, that's exactly right.
  1633. You know, I went over it really quickly,
  1634. so I'd forgive you if you missed it,
  1635. but there was a total fatalities column
  1636. that for each accident, like if I look at it here,
  1637. had how many people died in that accident?
  1638. Where is it?
  1639. Hold on, my Zoom toolbar is getting in the way.
  1640. Here, so one person died in this one,
  1641. two people died in this one,
  1642. three people died in this one.
  1643. So if we sum this whole column,
  1644. we'll know the total number of fatalities.
  1645. You're exactly right.
  1646. You have an idea about how we might write that code?
  1647. Only things we've done so far.
  1648. Maybe value sum.
  1649. Yeah, we're gonna use the sum.
  1650. We're gonna use the sum first,
  1651. but we have to access our column, right?
  1652. So we know our data, what's the name of our data frame?
  1653. Do you remember?
  1654. Accident list.
  1655. Accident list.
  1656. So if I type accident underscore list,
  1657. like if I just run that cell, it just prints it out, right?
  1658. Now you told me that we got,
  1659. we know the column that we wanna deal with, right?
  1660. It's total fatalities.
  1661. So if I do dot, and now if I do total fatalities,
  1662. like that, now if I just hit play there,
  1663. there it's just spitting out the numbers as a series, right?
  1664. Which is what we see here in the spreadsheet, right?
  1665. So now we've accessed our series.
  1666. Now what do we do next to get the sum?
  1667. Do you remember?
  1668. Dot sum.
  1669. Dot sum, I know.
  1670. Exactly, boom, dot sum, hit it.
  1671. There's the answer, 336 people died, right?
  1672. And so that's where we used the exact same technique
  1673. that we used with our list of two, four, six, and eight.
  1674. We used on this data frames column,
  1675. and no different code,
  1676. but we got an answer
  1677. to a pretty interesting journalistic question along the way.
  1678. Very good.
  1679. Okay, so the next question I have is,
  1680. which helicopter maker had the most accidents, right?
  1681. So we did the helicopter make and model
  1682. with the most accidents.
  1683. How would we get the maker, the company?
  1684. And how would we know which one had the most?
  1685. So let's look at our columns again.
  1686. We had this LA times make and model column, right?
  1687. Which we used before, but look,
  1688. there's another column called LA times make, right?
  1689. And that has not the maker and the model,
  1690. it has just the maker, right?
  1691. So these are the Bell helicopter company, Airbus,
  1692. this is Robinson.
  1693. So remember, we can access our columns
  1694. by typing in our data frame name,
  1695. accident underscore list dot followed by the column name.
  1696. So dot LA times make, there it is.
  1697. And just as we did before,
  1698. we can get a frequency or a group and count of the values
  1699. with the value underscore counts method.
  1700. So LA times make value counts, boom,
  1701. we see Robinson has had 58, Bell has had 43.
  1702. And just to quickly review,
  1703. that's the exact same thing we did here
  1704. with make and model, right?
  1705. And the only thing that's different in the code
  1706. is it's a different column, right?
  1707. And so by using the exact same technique
  1708. with a different column name substituted in the middle,
  1709. we get the answer to a different question, right?
  1710. But the exact same code really.
  1711. The sort of mix and match of your data and the techniques
  1712. is really a big part of how this works, okay?
  1713. So we can do the same thing
  1714. for the total number of helicopter accidents by year.
  1715. Can somebody tell me what to type to get that calculation?
  1716. You could look at the info report above
  1717. or preview the data table would probably help.
  1718. Don't make me call on you, somebody.
  1719. Let me try, can we use these value counts method again?
  1720. Yes.
  1721. By the column name of the year.
  1722. Yeah, so just tell me what to type and I'll try it.
  1723. Okay, first we need the name of the list accident
  1724. underscore list and dot year
  1725. and then dot value counts, right?
  1726. That's exactly right, you got it, man.
  1727. So there we have a really,
  1728. you'll notice that it's not in chronological order, right?
  1729. It's ranked by which year's had the most, right?
  1730. But we see that the year 2008
  1731. had the most accidents of 22, right?
  1732. And we also here kind of get a look at the range of values
  1733. in this column.
  1734. This gets to what Ryan was talking about earlier,
  1735. exploring your data.
  1736. This is where value counts can be really useful
  1737. to just quickly get a sense of like in this column,
  1738. what the heck is even in there, right?
  1739. What options are there,
  1740. especially when there's like categorical codes
  1741. or something like that, like the maker is an example.
  1742. It's nice to just kind of get a look at it.
  1743. And so there's another one.
  1744. All right, so if I wanted to do that now
  1745. for which state had the most helicopter accidents,
  1746. what would I type?
  1747. Yoshido, I bet you know the answer.
  1748. You wanna tell me what to type?
  1749. Maybe we can imply max.
  1750. Ah, nope.
  1751. And I don't think it will
  1752. because max will only run on number columns.
  1753. It might give us, what will it do?
  1754. Yeah, so it says, look at this crazy long error.
  1755. Oh my God, I hate computers, I quit, right?
  1756. You can see at the very end,
  1757. oftentimes the clues on your errors
  1758. are at the very, very bottom of this trace back,
  1759. which is frustrating, but that's how they do it.
  1760. And we can see here, it's saying there's a type error,
  1761. which means the data type is like not happy.
  1762. And it's saying, hey, I expected a float
  1763. and you gave me a string, right?
  1764. And so max, isn't it?
  1765. So do we have to use the value counts again?
  1766. We use value counts again, you got it.
  1767. I know, I wasn't very creative with these challenges guys,
  1768. but there you go, you see it.
  1769. California, America's most populous state had the most,
  1770. which is 16.
  1771. So here's where a rate might be helpful too, right?
  1772. But we see there it is, California number one.
  1773. All right, so that gets us to the end of that section.
  1774. We're now gonna move on to more advanced things
  1775. to do with your data frame,
  1776. things like filtering it to get down
  1777. to just records you're interested in.
  1778. Things like grouping it to group and count
  1779. and calculate more interesting statistics
  1780. than just frequency counts.
  1781. Things like merging it to other data sets
  1782. and calculating new columns,
  1783. which is gonna be a lot of fun.
  1784. It's probably about another hour worth of stuff
  1785. for us to go through, maybe a little less than that.
  1786. But I wanna just take it this moment now
  1787. and then maybe take a break.
  1788. We've been going, we're probably a little past the midpoint.
  1789. You can look here at my scroll bar.
  1790. You can see we're actually a little more like 75%
  1791. maybe of the way there, but it's been a lot.
  1792. And so I just wanna give everybody a chance
  1793. to go to the restroom if they need to
  1794. or refill their coffee cup.
  1795. And we can meet back here, let's say at 1132,
  1796. just to pick a weird number, at 1132,
  1797. we will resume and be right back.
  1798. In the meantime, I'm just gonna hang out here.
  1799. And so if you have any questions really about anything,
  1800. feel free to speak up.
  1801. All right, so 1132, I'll see you guys.
  1802. Ryan, how we doing?
  1803. Good.
  1804. Feeling good.
  1805. There's a good question from Cooper in chat.
  1806. I don't know if we wanna come back
  1807. and talk about how you might explore something like that.
  1808. Yeah, I haven't seen it.
  1809. Let me, I kind of lost my Zoom toolbar here.
  1810. Would you mind just, I hate,
  1811. would you mind reading the question?
  1812. Yeah, what's going on in Arizona?
  1813. What's going on in Arizona?
  1814. Yeah, exactly.
  1815. And that's what's kind of fun about,
  1816. just listening to the statistics, right?
  1817. Which is why I often think of it's,
  1818. to me, there's a sort of spiritual aspect of that sometimes,
  1819. but I'll restrain myself.
  1820. Yeah, what is going on in Arizona?
  1821. You know, we'll explore that when we get into filtering.
  1822. You know what I mean?
  1823. We might be able to dig into the records.
  1824. Like, you know, I often approach it exactly
  1825. like you are here.
  1826. It's like, I ask one question of the data,
  1827. I get an answer back,
  1828. and then I have a follow-up question, right?
  1829. And so then I write another piece of code
  1830. that helps me with the follow-up question.
  1831. In this case, I might wanna say,
  1832. what's going on with Arizona?
  1833. Let's look at the Arizona records, right?
  1834. So maybe in our next filter section,
  1835. we'll filter down to the records in Arizona
  1836. just trying to answer that.
  1837. Cause I'm paranoid, I don't know about you.
  1838. I'd be like, Arizona seems like the rate's kind of high.
  1839. Maybe there's some duplicates in there.
  1840. Ooh, you know what I mean?
  1841. Maybe Ben screwed it up, you know?
  1842. And like, the one way to prove that
  1843. is to actually look at the records, you know?
  1844. And so, I don't know.
  1845. I think, you know, just having written this story,
  1846. speaking from all I learned in my reporting,
  1847. I would just generally say there's certain parts of America
  1848. where helicopters are more common than others.
  1849. You know what I mean?
  1850. They're more commonly used as working helicopters
  1851. in some places than others.
  1852. Like, but then I think there's also places
  1853. where they're more popular recreationally, you know?
  1854. And California is definitely a place
  1855. where helicopters are popular.
  1856. In general, the West is a place
  1857. where helicopters are popular,
  1858. wide open spaces and all of that.
  1859. But I don't, mm-hmm.
  1860. I was just gonna say, I think this is a really good example
  1861. of kind of, like you said, listening to the data,
  1862. letting it tell you where to look for interesting stories.
  1863. Because I mean, I think the subtext here is,
  1864. yeah, California, Texas, those are like heavily popular.
  1865. Those are like some of the most populated states there are.
  1866. You probably would expect to see numbers be high there.
  1867. And then like Arizona, maybe just kind of your gut sense is,
  1868. that might be an outlier.
  1869. Like, to me, this is a good example of like,
  1870. I found something interesting in the data
  1871. and like my next step is probably picking up the phone.
  1872. Like, who could I call?
  1873. And ask like, why is Arizona,
  1874. I feel like an outlier here.
  1875. So, just a cool example, good question from Cooper,
  1876. like example of how to listen to the data
  1877. and find like interesting things
  1878. that might turn into stories.
  1879. Totally, that's it right there, that's it.
  1880. Picking up the phone is a great way to go.
  1881. Lot of phone calls on this story, man.
  1882. To clean up that make and model column,
  1883. I had to like learn so much about helicopters
  1884. because like the raw data from the NTSB
  1885. had like every odd variation of every helicopter model.
  1886. You know, like there, you know,
  1887. you call it the 106 Bell or whatever,
  1888. but there's the 106A and the 106B
  1889. and there's all these like little variations
  1890. or they would spell it different
  1891. or the companies would change names over time.
  1892. And so like, you know,
  1893. there aren't that many helicopters to be honest,
  1894. like compared to planes or cars,
  1895. they're pretty, you know, small amount of variety,
  1896. but I had to learn all of them to kind of to do the story.
  1897. And that was me just in a spreadsheet, you know,
  1898. like that spreadsheet we saw, that's me in the database,
  1899. just like going through,
  1900. doing a values count on the raw, ugly ones,
  1901. seeing what they are, spitting that out in a spreadsheet
  1902. and then going through row by row
  1903. and just being like, this is the R44,
  1904. this is the R2D and just like categorizing it.
  1905. And that like slow refinement of the data
  1906. was the real work here.
  1907. Yeah, I know you have taught classes
  1908. with political contributions data,
  1909. like that's just such a great example of like data
  1910. that has to have so much cleanup.
  1911. When we were talking before the class,
  1912. I pulled up some PDC stuff from here in Washington state
  1913. and just like looked at a local candidate
  1914. and there's like 18 different ways
  1915. that my town is spelled in the data.
  1916. Cause it's people entering stuff in a form
  1917. and like they don't care about getting it right,
  1918. it's not necessarily a drop down
  1919. and there's, you know, different capitalizations
  1920. and like all that cleanup work is so much of
  1921. then what lets you do stuff like this
  1922. as quickly as you're able to, you know.
  1923. And we're definitely, we're glossing that over today
  1924. in this presentation that in reality,
  1925. that just getting the data,
  1926. basically understanding what it is and isn't
  1927. and then cleaning up its problems is like in most cases
  1928. and very much so in this case are like 90% of the work,
  1929. you know, and then like 1% was doing the analysis
  1930. and then the last nine were bulletproofing
  1931. and making sure I didn't screw it up.
  1932. Yeah.
  1933. Yeah.
  1934. Has anyone here flown in a Robinson helicopter?
  1935. I'm always curious to know.
  1936. I've never been in a helicopter.
  1937. Neither have I.
  1938. I do have my like news copter fantasies, Ryan.
  1939. I just got a, it's got a drone license
  1940. and that's the closest I'm gonna get I think,
  1941. but like my news fantasy for 2023 is that like,
  1942. I shoot some random local news event
  1943. and like get it on the 10 o'clock news.
  1944. You think I can do that?
  1945. We just got to get two drones that are big enough
  1946. for you to put one foot on each and then you're off.
  1947. All right, I'm gonna mute.
  1948. We're at alumni.
  1949. All right, we're at our restart time.
  1950. Thank you for your patience, everybody.
  1951. We're gonna now pick it back up
  1952. and we're gonna follow this through all the way to the end.
  1953. So our next step is filtering the dataset.
  1954. How do I take my table and how do I narrow it down
  1955. to just the records I'm more interested in
  1956. or that I wanna analyze and look at?
  1957. And pandas has its own technique for doing this,
  1958. which is it's called indexing or filtering
  1959. that we're gonna cover next.
  1960. This is if you've ever done database software like SQL,
  1961. this is really very similar, right?
  1962. And to the where statement in SQL,
  1963. you have to make what's called an expression that's a test
  1964. and that test is then applied to every row in the data frame
  1965. and things that are true are kept after the filter
  1966. and things that are false are let go, right?
  1967. And the result is that you just keep all the things
  1968. that are kept and that's the filter, right?
  1969. And every expression has sort of the column
  1970. or the series that you're gonna evaluate,
  1971. the test that you're gonna run against it.
  1972. Does it equal this?
  1973. Is it greater than this or less than this?
  1974. There's these different kinds of tests.
  1975. And then there's the value you're gonna test it against.
  1976. And so a really common filter on a string field
  1977. is I wanna filter down the data frame
  1978. to only the records where a certain column
  1979. equals a certain value, right?
  1980. And that's the example we're gonna look at here.
  1981. The example I wrote is for the state of Iowa,
  1982. but because Cooper had this great question about Arizona,
  1983. I'm gonna change it to Arizona, right?
  1984. And so we see I've created a variable,
  1985. it equals AZ for Arizona, I'm gonna run it
  1986. and now that's saved right there, right?
  1987. That's easy, that's the easy part.
  1988. That's just saving the variable, my state equals Arizona,
  1989. right?
  1990. Now here in the next cell is the hard part.
  1991. This is how you actually do a filter in pandas.
  1992. You start off by naming the data frame, right?
  1993. That's the data frame I wanna filter.
  1994. And then you do these flat bracket.
  1995. The flat bracket is saying, I'm about to do some indexing.
  1996. I'm about to do some filtering, right?
  1997. That's how you tell that to the data frame.
  1998. And then inside of the flat bracket
  1999. is that expression I was talking about.
  2000. And it's a little overwhelming at first,
  2001. but if you just stop and read it slowly,
  2002. you'll get it pretty quick,
  2003. which is you have your first thing,
  2004. which is the column that you wanna evaluate.
  2005. So you have to repeat the name of the data frame
  2006. to access the column, right?
  2007. So the column is state, right?
  2008. So I have to do data frame.state to get the column.
  2009. I know it's dumb, you have to repeat it.
  2010. That's just how pandas works, I'm sorry, right?
  2011. Second is the operator that you wanna run.
  2012. In this case, you wanna test
  2013. whether something equals something else.
  2014. And the way you do that in Python
  2015. is two equal signs in a row.
  2016. That's because one equal sign is used to assign variables.
  2017. So they can't use it here.
  2018. The computer's strict, it's dumb.
  2019. Expressions can only do one thing, right?
  2020. So one equal sign is for assignment.
  2021. Two equal signs is for evaluation of an operator.
  2022. Is one thing equal to the next thing, right?
  2023. So our thing we're testing is the state column.
  2024. Then we're saying, is it equal to the variable state?
  2025. Is it Arizona, right?
  2026. So if I run that, boom, we've got our narrowed list
  2027. that now is only the records in the state of Arizona, right?
  2028. If I look at the state column, it's AZ, AZ, AZ, right?
  2029. And if I were to change that back to Iowa,
  2030. where I grew up, and I were to run those two cells again,
  2031. we'd see the list got a lot shorter, right?
  2032. It only has three now, and it's IA, IA, IA, makes sense.
  2033. Pick another state, run it for yourself.
  2034. Go back to AZ, get it, run it again.
  2035. Now to save that for use later,
  2036. of course you can save the result
  2037. of your filter to a variable, right?
  2038. And so if I named it, I was going to call it myaccidents,
  2039. but I'm going to call it AZaccidents.
  2040. I'm going to change the name of that variable.
  2041. I'm riffing, I'm off-roading, right?
  2042. And I'm going to say AZaccidents
  2043. equals the result of the filter.
  2044. And now that is now a new data frame, right?
  2045. With fewer records, and I can take a look at it
  2046. using those same things we did before,
  2047. which are head and info.
  2048. Remember those guys?
  2049. There they are.
  2050. And we can now see when we look at the row count
  2051. in the info report, there's only 14 rows, right?
  2052. And that 14 rows matches the data frame.
  2053. And that 14 rows matches what we got
  2054. with our value counts up here, right?
  2055. Because we filtered down to see just those 14.
  2056. Now you could write that out to a spreadsheet
  2057. and you could look at it in Excel if you wanted to study it,
  2058. like look for problems, like I was saying.
  2059. You can just sniff around here,
  2060. but you've got something you can kind of play with now.
  2061. That's filtering.
  2062. There's other expressions, greater than, less than,
  2063. which are often useful when you're evaluating number columns, right?
  2064. I only want campaign contributions greater than $1,000
  2065. or something, right?
  2066. Would just be like amount greater than 1,000, right?
  2067. If I had a column called amount, which I don't in this case,
  2068. but you get my point.
  2069. But we can say total fatalities greater than one.
  2070. So we can say we want, you know,
  2071. we want all helicopter accidents
  2072. where someone other than the pilot died, right?
  2073. This might give us that if we think about it, right?
  2074. Because anything has more than one fatality.
  2075. Well, I guess the pilot could survive and the passenger could die.
  2076. So that's not true.
  2077. But like, you know, we want,
  2078. if we wanted all helicopter crashes with more than one death,
  2079. this is what would do it, right?
  2080. And that's me struggling through it.
  2081. I want to answer this question.
  2082. How do I like, and then it helps you think about it.
  2083. Well, I didn't ask the question, right?
  2084. You know, and then I had to loop back.
  2085. That's filtering.
  2086. Any questions about filtering?
  2087. So I'm just going to throw out the fact that you renamed that
  2088. variable as the accidents.
  2089. I offer.
  2090. Yeah, no.
  2091. But I just want to throw out that that means that you still have
  2092. access to that original, like the original filter.
  2093. Also, like something that I do to myself all the time is
  2094. overwrite a variable that I was going to use again.
  2095. And like my debugging process,
  2096. often I run into things that I just, I was just really dumb and,
  2097. and it's got moving too fast and by and I just reused a variable name.
  2098. Yeah.
  2099. You know, a few different places in my code.
  2100. And all of a sudden I'm ending up with different results that I'm
  2101. expecting. And so renaming variables,
  2102. like AZ accidents like that is just a really helpful way.
  2103. Like it may feel like I'm getting cluttered.
  2104. I got too many variables.
  2105. Your computer doesn't care.
  2106. And it's just a really nice way to make sure that I'm not accidentally
  2107. overriding something that I might want to use and rely on later on.
  2108. That's totally true.
  2109. It's totally true.
  2110. Yes.
  2111. And that's where, you know, if I,
  2112. especially if I no longer need a variable,
  2113. I like to delete the cell too.
  2114. You know what I mean?
  2115. Just to like get it out of there.
  2116. So I'm not accidentally using it.
  2117. It's kind of, it's pretty easy,
  2118. especially when you're doodling and you start to kind of get a little
  2119. confused about what's where it's normal.
  2120. It happens to everybody.
  2121. It's part of the process.
  2122. That's filtering.
  2123. All right.
  2124. We're going to move on to grouping.
  2125. So grouping is when you take a column and you,
  2126. you reduce it to like the number of unique values in it to groups.
  2127. You know what I mean?
  2128. And items.
  2129. And then you do something with those groups.
  2130. We already did this with value counts, right?
  2131. And value counts.
  2132. We were able to take our make and model column.
  2133. And with that nice shortcut,
  2134. we were able to group up how many there are.
  2135. Right.
  2136. And that's because value counts is designed to be a shortcut.
  2137. Right.
  2138. But let's say you want to do something that's a little more custom or
  2139. isn't just counting frequency,
  2140. but maybe is calculating statistics.
  2141. Right.
  2142. To do that,
  2143. you have to write your own group functions that have a little more
  2144. going on.
  2145. And that is just another dot method that's hooked on to every data
  2146. frame.
  2147. Every data frame has dot group by right there available.
  2148. And you could pass into the parenthesis,
  2149. the name of the column you've won a group.
  2150. And so LA times make and model is what we did to count accidents by
  2151. model.
  2152. And so I'm going to group on that, which does nothing.
  2153. It's just, it's just grouped it,
  2154. but you haven't given it an aggregation function,
  2155. something to actually do with each group.
  2156. Right.
  2157. So to start, it just spits out.
  2158. Here's groups.
  2159. It doesn't do anything yet.
  2160. It's expecting you to change something else on the end,
  2161. which is a whole other set of dot functions.
  2162. So if I do dot tab again for autocomplete,
  2163. it's not working for some reason.
  2164. Yeah.
  2165. Okay.
  2166. Um,
  2167. there's a whole bunch of them there.
  2168. So dot size is the equivalent of value counts.
  2169. So if you do a group and you do dot size,
  2170. it's actually the same thing as value counts.
  2171. It doesn't rank it because, you know,
  2172. but value counts is kind of a shortcut for basically this, right?
  2173. Which is just count the frequency of items,
  2174. the number, right?
  2175. But let's say we want to do something more statistical,
  2176. like we want to sum the number of fatalities per model.
  2177. So I know the R 44 had the most accidents with 38 accidents.
  2178. But did it have the most deaths, right?
  2179. To do that,
  2180. we would need to do something we did earlier,
  2181. which is sum the fatalities,
  2182. but we don't just want to sum the whole set.
  2183. We want to do a different sum for each one of these groups.
  2184. Right.
  2185. And so that's just as simple as chaining onto the end,
  2186. a technique that we already used elsewhere, right?
  2187. Which is do dot the name of the column,
  2188. followed by dot and the name of the mathematical function,
  2189. which was some.
  2190. So it's just the accident list dot group by dot column name dot sum.
  2191. And we get back, boom,
  2192. the total number of fatalities for each make and model.
  2193. And we see that the Robinson R 44 had 38 fatal accidents
  2194. for a total of 71 fatalities, right?
  2195. And we see here that the Airbus actually had more fatalities.
  2196. That's interesting.
  2197. It's a bigger helicopter, guys, right?
  2198. Mm-hmm.
  2199. Now, you know, that same thing gets spit out as this ugly series.
  2200. Again, reset index is our friend.
  2201. Just packing that up the end can give you a nice data frame, right?
  2202. So I've grouped the size,
  2203. which is just the count for each one,
  2204. and I've reset the index.
  2205. And so that's now created a data frame
  2206. that has each of our models, right?
  2207. Oops.
  2208. And the frequency count.
  2209. But we can see that there's kind of a gnarly thing about pandas
  2210. that it names that size column zero,
  2211. which is just a dumb thing about pandas.
  2212. So we're going to want to clean that up in a minute.
  2213. Because if we think about our ultimate bowl of calculating the accident rate,
  2214. the number of accidents per make and model
  2215. is going to be one of our inputs to calculating that rate, right?
  2216. So we want to kind of polish up this table
  2217. that's like our sort of finished finding
  2218. about the total number of make and model for each.
  2219. And so what I'm going to do here is just try to turn this like rough cut
  2220. into like a finished data frame
  2221. that's like the result of this stage of the analysis.
  2222. And so I've got my group by, I've got my size, I've got my resize index.
  2223. This is basically what I'm going to want to keep
  2224. to take over to the survey data.
  2225. I take that same code, I now save it to a new data frame,
  2226. which I call accident counts, right?
  2227. So now accident counts is what we see right here,
  2228. but it's got that ugly column name, right? Zero, you know?
  2229. So I want to rename that something a little more readable for later.
  2230. And you can do that with a function called rename.
  2231. So every data frame has a function called rename.
  2232. It expects you to give it a keyword argument, which we haven't done yet,
  2233. but these are basically options that you pass into functions that have names.
  2234. And you can do them in whatever order you want, which is kind of nice.
  2235. And most of them you never even need to know about.
  2236. They're really only there when you want to override something.
  2237. But when you want to rename a column in pandas,
  2238. there's a couple of ways to do it.
  2239. And this is one of them is you do the rename function on the data frame.
  2240. You say the columns input is equal to a dictionary,
  2241. which we haven't seen yet either,
  2242. which is a way Python sort of pairs together two different values.
  2243. There's a key and then there's a value and those two things are linked.
  2244. And you can have a lot of them in a dictionary, you know?
  2245. And so here zero, which is the name of the column that already exists,
  2246. I want to rename zero.
  2247. So I'm saying, hey, zero, I want to rename you to accidents.
  2248. And then in place is a pandas function that's in many, many, many of the pandas functions.
  2249. You're going to see it all over the place.
  2250. And it basically says, make this edit to the data frame in place
  2251. so that we don't have to rename or reassign the variable.
  2252. This can be dangerous,
  2253. but I just wanted to show it to you because it's very common in pandas is to say,
  2254. I want you to do this operation and do it in place
  2255. so that the edit to the data frame is saved immediately, right?
  2256. So if I run that and then I run my friend head again,
  2257. you can see that that column has now been renamed accidents.
  2258. It's no longer named zero, right?
  2259. And accident counts is kind of our finished analysis
  2260. of the total number of accidents per make and model
  2261. that we now want to take to our survey data,
  2262. which has how many hours in the air each helicopter spent
  2263. to then adjust and normalize to calculate our rate.
  2264. Any questions about group by?
  2265. You can group by more than one column,
  2266. which is done by just passing a list into the group by function.
  2267. Like so, and then you could give it a second column here, right?
  2268. Very common.
  2269. Okay, now we're going to move on to another fundamental operation
  2270. on databases, which is merging.
  2271. Merging is where you take two different tables
  2272. and you link them together using a common column.
  2273. This is a great example of where getting into computer programming
  2274. can be really valuable because while you can do this
  2275. in spreadsheet software like Excel or Google Sheets,
  2276. it's not that easy.
  2277. It's kind of a hassle, right?
  2278. This is now we're getting into this is where coding can really help.
  2279. And a lot of larger databases are often intentionally structured
  2280. to have many tables, right?
  2281. Now, in this case, we have two tables that we're bringing.
  2282. We have on the one hand, the make and model
  2283. and the accident counts, which we've just generated with our group by.
  2284. And then we have on the other, the FAA survey,
  2285. which I've tidied up and prepared for class
  2286. based on a Freedom of Information Act request
  2287. and a lot of documents I got back from the FAA.
  2288. But we can see here that this also has a column
  2289. called LA Times make and model and total hours.
  2290. And so for us to calculate our rate,
  2291. we need to merge or join these two tables together
  2292. so that one number can be divided into the other, right?
  2293. And that's what we're going to do in this next section.
  2294. We're going to read in FAA survey as a new variable
  2295. using our friend from earlier, read CSV.
  2296. So read CSV, which we use to read in the accidents table,
  2297. can be used to read in the survey table, right?
  2298. And all the differences is just the file or name right there.
  2299. It's the same thing all over again, just a different file,
  2300. which is the name of the file here in the system.
  2301. And hit that and play.
  2302. And now, if I were to run survey.info, which is below,
  2303. we can see here's the info report on that,
  2304. which is there's 12 rows, there's two columns.
  2305. One is a string or object, the other is an integer.
  2306. And accident counts, which we created before, very similar,
  2307. also has 12 rows, right?
  2308. Has make and model and accidents.
  2309. And what we want to do is we want to merge these two together
  2310. or what in other tools is called join them using their common column,
  2311. which is make and model.
  2312. So all is really good to look careful at your tables before you join,
  2313. so you know which columns need to match.
  2314. You know how many rows are in each one.
  2315. It's often common for there to be more rows in one table than the other.
  2316. And you have to work out what you want to do with the ones that don't match,
  2317. right?
  2318. But in this case, we don't have that issue.
  2319. I took care of it ahead of time.
  2320. But you can think that through by just looking at it there.
  2321. So to do the merge, there's another one of pandas tool belt functions,
  2322. dot merge, right?
  2323. So PD, which is pandas, right, dot merge.
  2324. And then we do our parenthesis because we're calling a function.
  2325. Now merge can take multiple inputs because it's a little more complicated.
  2326. It wants two data frames to put together separated by commas.
  2327. So our first data frame, which is often called the left table in the join,
  2328. is accident counts.
  2329. And our second data frame, which we just input imported right here, right,
  2330. is called survey is our second table.
  2331. That's our right data frame is what it's known.
  2332. And then those two things need to be merged.
  2333. And you can give the instructions to merge on which column to use to join the
  2334. tables with the on argument.
  2335. So on equals and then in quotes the name of the column,
  2336. which we see right here.
  2337. All right.
  2338. And then I'm going to save the result of that to a variable merged list.
  2339. Boom.
  2340. Just it's also important to inspect your table after every merge to make sure
  2341. rows didn't get dropped due to joins being missed or anything unexpected
  2342. happened.
  2343. And so I will usually run ahead and info immediately afterwards just to make
  2344. sure things are what I expect.
  2345. So we see here we now have for the make and model.
  2346. We have both the accident count and the total hours sitting side by side in
  2347. a new connected table.
  2348. Right.
  2349. And we can see that there are 12 rows in it, which is exactly what I would
  2350. expect.
  2351. Commonly, if there was an error or something you didn't want, this number
  2352. would be less than 12 or more than 12,
  2353. which might be in the joint that some of the values appeared more than once or
  2354. didn't appear on one side of the joint or the other.
  2355. That's it.
  2356. That's merging.
  2357. Any questions about that?
  2358. No.
  2359. OK, we're going to move on to computing a new column.
  2360. Remember, our mission is to calculate a rate.
  2361. Right.
  2362. And so that means to adjust the number of accidents for the number of hours
  2363. that each helicopter was in the air to try to make it fair to compare
  2364. helicopters that were flown more than others.
  2365. Right.
  2366. Now, that's just generally a good practice when it comes to comparing things
  2367. of different sizes or different frequencies.
  2368. Right.
  2369. But in this case, it also was to vet a claim being made by John Robinson,
  2370. CEO of the Robinson Helicopter Company,
  2371. who told me and Kim Christensen that, yeah,
  2372. we have the most accidents in our helicopters,
  2373. but we also have the most pilots in the air, dude.
  2374. So your story is crap.
  2375. You know, don't even try running that.
  2376. I got lawyers, you know, that kind of thing.
  2377. That's one exact quote.
  2378. But, you know, that was the kind of vibe of it.
  2379. He definitely explicitly said that the reason they have more accidents is they
  2380. have more helicopters in the air, which is true.
  2381. But to evaluate whether his claim was true,
  2382. we could calculate a rate.
  2383. Right.
  2384. And it wasn't just that I invented this idea of coming up with the rate.
  2385. One, he had made a claim and I was checking it.
  2386. Right.
  2387. And two, there actually previously had been an FAA study done in the
  2388. 1990s, about 20 years before they use this exact same methodology.
  2389. The FAA study took the accident numbers.
  2390. They took the flight hours from this exact same survey and they calculated
  2391. rates.
  2392. So I was able to basically replicate a method that the government itself had
  2393. already endorsed as a safety check,
  2394. which is really great way to approach designing a data story because it
  2395. takes a little bit of the it takes away the factor of like you're just in
  2396. the LA times or your outlet is just kind of inventing a standard.
  2397. It's holding people to write in this case.
  2398. This was a standard that the government itself had established and had used
  2399. in formal safety tests in the past, though it had been a long time.
  2400. And to the CEO of the helicopter company had made a claim along these same
  2401. lines.
  2402. And so for that reason, the analysis was sort of support,
  2403. you know, had more contextual support that maybe is boring.
  2404. I should stop talking about, but FYI,
  2405. I think that type of design is really important to crafting effective
  2406. investigative stories.
  2407. All right.
  2408. So how do we actually calculate it, which is probably more what you want to know?
  2409. You just get back to a lot of the basics that we've already covered so far,
  2410. just returning in a slightly different way.
  2411. Right.
  2412. So we know that we have this new table that has these three columns,
  2413. LA times make and model accidents and total hours and calculating a rate is
  2414. as simple as dividing the accidents into the hours.
  2415. Right.
  2416. And we know we can access the columns as series.
  2417. Right.
  2418. So it's calculating the over calculating the rate for each column.
  2419. Each row is as simple as taking the accident series and the hour series and
  2420. putting the math operator in between the two.
  2421. It's that easy.
  2422. Right.
  2423. And then you run that cell and you can see there.
  2424. Boom is a series that's calculated the rate for every row in our table.
  2425. Right.
  2426. And this is the same thing that we've done for one million rows.
  2427. Right.
  2428. Because pandas is cool.
  2429. But we want to save that as a new column on our data frame.
  2430. We want to calculate a new column and stick it on there at the end and save it
  2431. in the same way that you might calculate a column and Excel with the formula.
  2432. Right.
  2433. And the way you do that is just with the same assignment trick that we had
  2434. before.
  2435. When you add a new column and pandas,
  2436. it's good practice to use this flat bracket approach.
  2437. Right.
  2438. So that we showed earlier.
  2439. So if you do your data frame, a flat bracket, quote,
  2440. and whatever you want to name it,
  2441. this will create a new column on your data frame that will be the result of
  2442. whatever is on the other side of the equal side.
  2443. And so here I'm taking that exact same math operation.
  2444. I've just pasted it down here and I'm saving it to a new column that I'm
  2445. going to call per hour.
  2446. Right.
  2447. So that's accidents per hour in the air flight hour.
  2448. And if I run that nothing comes out because it's been saved.
  2449. And now if I do head boom per hour.
  2450. There it is.
  2451. Right.
  2452. We've now calculated a new column tacked straight on the end.
  2453. Now you can notice that this has this, this stuff at the end.
  2454. Ryan, do you know what that is?
  2455. E equals oh seven.
  2456. Uh, I can't tell you what the number is, but it looks like a scientific
  2457. notation to me.
  2458. Pop quiz, pop quiz, Ryan.
  2459. Yeah, exactly.
  2460. It is scientific notation,
  2461. which is just a fancy way of taking zeros off the front end of a number and
  2462. kind of moving the, the, the most,
  2463. the first significant value to the front, right?
  2464. Is kind of what that does.
  2465. It's not very easy for us humans to read unless we're super nerdy.
  2466. Uh, especially me, I should speak for myself.
  2467. It's not easy for me to read.
  2468. Maybe you guys have an easy time with it.
  2469. And so one common way of dealing with this issue when calculating rates,
  2470. you see this with homicides.
  2471. You see this with all kinds of rare events is to just take it times.
  2472. Uh, a number that starts with 10 and ends with zeros, right?
  2473. Depending on what's kind of appropriate.
  2474. You see this all the time with per capita statistics about things per
  2475. million people or a hundred thousand people or 10,000 people calculating
  2476. rates really commonly to do this.
  2477. And it has no effect on the actual number.
  2478. It's just a way of, um,
  2479. of framing it that makes it a little easier for our human brains to parse.
  2480. Right.
  2481. And, um, and so we want to do per a hundred thousand hours is how we
  2482. decided to do it,
  2483. but there's really no difference between that and 10,000 or a million.
  2484. It's just kind of what works for the data and makes sense for them,
  2485. how you're going to frame it.
  2486. And that is as simple as what you see here.
  2487. So we have the exact same mathematical operation that they created the per,
  2488. uh, hours column.
  2489. I've put parenthesis around it just so that those will run together first,
  2490. just like in elementary school math class.
  2491. Right.
  2492. And then after the parenthesis, I've done the asterisks,
  2493. which is multiplication.
  2494. And I've just put the number 100,000 right there.
  2495. One underrated Python thing is you can put an underscore there to make the
  2496. number more readable. And it still works.
  2497. I like secretly love that trick.
  2498. Um, and so it's just going to take the result of it times 100,000.
  2499. And I'm going to save it to a different column,
  2500. which I'm calling per 100 K hours.
  2501. And I run that.
  2502. I should put a head in here, which I didn't do.
  2503. So I'm going to add a new cell, do a head.
  2504. And we can now see that there is this new column.
  2505. Right.
  2506. And so we can say for the Augusta one Oh nine, make it model.
  2507. There were two accidents in 360,000 hours,
  2508. which works out to a half an accident per 100,000 hours,
  2509. which still isn't that readable. You know what I mean? Honestly,
  2510. like per million hours might be more readable for the average person,
  2511. maybe.
  2512. But I kind of felt that because a lot of these didn't have a million
  2513. hours,
  2514. it wouldn't be appropriate to do that adjustment is kind of what I felt
  2515. because it would be sort of inflating it, not inflating it,
  2516. but it would, you know,
  2517. it would be a step of the scale a little further than what the data is
  2518. at, you know, but again, all that is subjective.
  2519. That worked for everybody.
  2520. Any questions about calculating new columns or computing new columns?
  2521. I never know what to call this.
  2522. Is computing the right word, Ryan?
  2523. What do you call this when you do this?
  2524. Yeah, that's, I think that's a fair term to use.
  2525. You're running a computation and saving results into a column.
  2526. There you go.
  2527. So congratulations, everyone. You are now computational.
  2528. You can put that on your resume.
  2529. Okay. Now we're getting to our last section, believe it or not.
  2530. Right. And this is about sorting.
  2531. This is another really common practice, you know,
  2532. and spreadsheets with data is to use one of your columns and to just
  2533. resort the data frame according to that column in one direction or the
  2534. other.
  2535. And in our case, you know,
  2536. our goal is to calculate which helicopter make and model has the
  2537. highest accident rate, right?
  2538. And our per hundred cake hours column is going to be a way to approach
  2539. that.
  2540. And all it is, is taking your data frame,
  2541. which against merged list,
  2542. the dot operator that we've been using all day,
  2543. and then this new function we haven't used before,
  2544. which has a really verbose and annoying name.
  2545. It is sort underscore values.
  2546. I've always thought, why not just dot sort?
  2547. Well, I don't know.
  2548. They just decided some nerd years ago,
  2549. decided to write it long and now we're all stuck with it.
  2550. So it's dot sort underscore values,
  2551. and then it needs the input.
  2552. What column to sort by, right?
  2553. Again, you put that in quotes.
  2554. And if I run that, we'll see there's the result, right?
  2555. So here's our table that has been resorted according to this column.
  2556. And if we look at it, it's getting higher as we go down.
  2557. And that's because by default,
  2558. things are sorted in ascending order,
  2559. which is from lowest to highest when it's a number
  2560. or from A to Z when it's a object or string, right?
  2561. And so there's just another technique for reversing this
  2562. to sort in descending order, which I'd like to do here.
  2563. This is also very verbose, which I find annoying,
  2564. but you have to do comma ascending,
  2565. which is the option that was defined by the pandas developer equals.
  2566. And we haven't seen this yet,
  2567. but in Python there are these things called Boolean operators,
  2568. whether something is true or false.
  2569. So it's a data type, right?
  2570. Something can be true or false or not exist,
  2571. which is called null or none in Python is what it's actually called.
  2572. And so if you say ascending equals false,
  2573. which is a little bit of a brain bender,
  2574. you're basically saying do it in descending order, right?
  2575. Ascending isn't false.
  2576. And that will reverse the sort so that now the highest values come first.
  2577. We've sorted by scandalous is what I call this in the newsroom, Ryan.
  2578. Sort by scandalous.
  2579. Right.
  2580. And we see that they're number one,
  2581. a one with a bullet is the Robinson R44 helicopter,
  2582. which is at 38 accidents in 2.3 million hours,
  2583. which comes out to 1.6 accidents per a hundred thousand hours.
  2584. Number one of all make and model over a long period of time,
  2585. contrary to what the CEO of the company says.
  2586. And in line with estimates done in the 1990s,
  2587. 20 years ago showing there's been little improvement in the Robinson's
  2588. performance in this metric, which is the subject of our story and blah,
  2589. blah, blah.
  2590. You can read it.
  2591. That's it.
  2592. And so we have now,
  2593. I guess at almost exactly the two hour mark since we began completed this
  2594. tutorial.
  2595. This is the first time that this has been taught with this dataset and in this
  2596. way.
  2597. So I'm sure I screwed up all kinds of stuff.
  2598. And my hope is now maybe we could have a brief conversation that can answer
  2599. any questions that you all have.
  2600. And also you can give me maybe some feedback and criticism about what I've
  2601. messed up.
  2602. But before we do that,
  2603. I guess I think I would ask Ryan to maybe stop the recording so that people
  2604. could feel free to speak frankly,
  2605. without knowing they're going to end up on YouTube, you know?
  2606. Yep, you bet.
  2607. I'm going to push stop.
  2608. Thanks Ryan.

Downloads

Recording video · Timestamped transcript