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