#100DaysOfCode in Python Transcripts
Chapter: Days 37-39: Using CSV data
Lecture: Demo: Answer the questions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright we're ready to answer the questions, let's go through this again. I'm going to move this down just so it fits right here,
0:07 so we initialize the data. Boom, we're doing that. The next thing we need to do is say research dot let's say hot days.
0:15 Now what we're going to do is we're going to write a function here that is going to give us all the days, hottest to coldest.
0:24 And we come over here and say days equals this, then we could loop over and say for d in days, and that would show all of them.
0:32 But we want just the top five, and so the way you do that in Python is using something called slicing. So you can come over here,
0:39 and say I would like to go from index 0 to index 5. Alright so this gives us a little subset of our thing,
0:47 and when it starts at zero or ends at the length you can just omit it. So we can write it like this, and that will process the first five of em'.
0:56 And then let's just say, print d for a second. So we're going print out what that looks like. So let's go write this hot days function.
1:06 Alright so we want to go through our data, and figure out the hot days. And it turns out, the easiest way to do this is just to sort it.
1:13 So we can say return sorted data. Now that's going to sort it by, jeez I don't know, maybe the
1:19 first element, alphabetical, by date, I'm not really sure. So we want to control what it's sorted by, and in here we can say there's a function
1:29 that is going to take basically one of these items, one of these rows, and tell us what we're going to use to sort for it.
1:37 So we're going to say key equals lambda this is a little in line function. Lambda says there's a function, then the next thing we put is the argument.
1:45 So let's say r for record, we do a colon and then we have the body of the function, the implementation. Where do we want to sort by for hot days?
1:54 Let's say with a max temperature for the hot days. Actual max temp is what we want. So we're going over here and I'll say r.actual_max_temp.
2:05 Now this is close, this is going to actually give us the lowest value first, and then the highest value to the end.
2:14 So the default sort is going to go from low to high, how do you reverse it? There's two ways, we could say reversed=True,
2:24 I'll spell it correctly, that's one way, or another way a little more flexible, is to just put a negation here and say
2:31 multiply that by a negative number. So take your pick, you can do it either way. While we're here let's write the cold days function,
2:39 that should be easy. On the cold days take away that. And let's do wet days. And this time we're not going to sort by that,
2:50 we're going to sort by actual precipitation. And again wet days are where we have more, so we want to sort that in reverse.
2:57 Now the format is a little off so reformat the code. So PEP 8 is happy. Alright so those three functions actually should do what we need.
3:06 So let's go over here and we'll just print out the five hottest days. Now this is probably not how we want to view it,
3:12 so let's print this out slightly differently. So I would actually like the index, like this is the number 1 hot day,
3:17 this is number 3 hot day, so I can come over and say idx, and instead of just looping over the days, I can innumerate them and then loop over.
3:25 And that will give us the index and the day, for each time. So then in here we'll put something like a little dot to
3:32 say what day it is and we'll say the temperature in terms of Fahrenheit on such and such day. And we'll just do a little string format on this.
3:40 So what this is idx and it's zero base, you don't talk in terms of 0 1 2, you talk in 1 2 3 so plus 1. And then we need the day, dot.
3:50 Now notice we're not getting any help here, let's go back to these real quick. So we can come over here and tell Python this is a record.
3:59 Now if we do that, sorry not a record, you can tell it it's a list of record. Now this is a Python 3.5 feature,
4:08 and if we come over here we import this typing.List. So at the very top, we have from typing import List,
4:16 and we put this here, and I'll go ahead and while we're here just put it on the others because they're all the same type.
4:22 We come over here and now we go back to this, and I say let's try that again, d. Oh yeah, now our editor is smart, now it can help us.
4:30 What we want here, we want the actual max temperature, and then we want d.date. Alright, let's go and run it,
4:38 see if this whole thing's hanging together. Boom, look at that, hottest five days, 96-94-92-91-90. And those are your dates, that's awesome, right?
4:49 See how easy it is to answer the question. The challenge was not actually answering that question, the challenge was taking the data, reading it in,
4:57 and then converting it to a workable format and storing it in our record. Once it's stored like that it is easy.
5:04 Let's go ahead and do the same thing for the coldest days. So we'll say that days is not the hot ones but now it's
5:12 research.cold_days, should do exactly the thing. And let's do a little print in between just so there's some spacing, and you guessed it,
5:22 the wet days, same thing, just ask for the wet days. Let's run it, so there's our hot days, the cold days look really cold,
5:29 well doesn't look that cold does it? Did I get the cold? That might be the high on the cold days. Let's go down here yeah. Yeah, yeah, careful.
5:38 This going to be actual min temp, like so, and this is going to be actual precipitation. There we go. Let's say, inches of rain, there we go.
5:55 Oh yeah, now that's starting to look cold isn't it. 23 Fahrenheit, that's like negative negative 5 Celsius
6:00 for those of you who are on the Celsius scale, this is like 36-37. Alright so pretty hot, pretty cold. I notice the sorting is correct.
6:10 The weather stays, the heaviest amount of rain during that time was 2.2 inches, or 5 centimeters. And then it goes down pretty quickly from there.
6:20 So hopefully you really got a good sense of how to take the CSV file in, read it, parse it, convert it to a usable format
6:29 and then just quickly answer questions. We stored it in a list and sorted the list, there might be other data structures you need to use for your data.


Talk Python's Mastodon Michael Kennedy's Mastodon