1 00:00:00,400 --> 00:00:02,950 To add up the values in one column for 2 00:00:02,950 --> 00:00:08,260 this data set we want to loop through the list, the outer list, data from CSV, and 3 00:00:08,260 --> 00:00:13,420 then grab the third column in the inner list each, each time we loop through it. 4 00:00:13,420 --> 00:00:14,965 So, let's create a function that would do this. 5 00:00:14,965 --> 00:00:20,721 [SOUND] Gonna import 6 00:00:20,721 --> 00:00:29,357 our functions from before. 7 00:00:29,357 --> 00:00:34,852 [SOUND] We're gonna pass in an argument for 8 00:00:34,852 --> 00:00:40,504 the data sample, and then, within this, 9 00:00:40,504 --> 00:00:46,321 we wanna have a temporary total t, number. 10 00:00:46,321 --> 00:00:47,630 We wanna return the total. 11 00:00:51,360 --> 00:00:56,815 And let's loop through the outer list to get each, each row. 12 00:00:56,815 --> 00:01:03,471 [SOUND] Now we're gonna go ahead and see starting from the second row. 13 00:01:03,471 --> 00:01:05,430 That way we can skip the header. 14 00:01:09,190 --> 00:01:13,930 Although price is gonna be row 2 and then we're gonna make 15 00:01:13,930 --> 00:01:18,570 it a float type just to make sure that we can work with it as a number. 16 00:01:20,160 --> 00:01:26,146 Each time we go through the list we're just gonna add that to the total. 17 00:01:26,146 --> 00:01:30,879 [SOUND] We have here 18 00:01:30,879 --> 00:01:35,296 that is says 0, 19 00:01:35,296 --> 00:01:41,606 which is not what we're 20 00:01:41,606 --> 00:01:48,548 expecting so obviously, 21 00:01:48,548 --> 00:01:52,972 I have an error. 22 00:01:52,972 --> 00:01:57,450 Let's actually make the total be the sum of price. 23 00:02:00,740 --> 00:02:02,871 There, that looks better. 24 00:02:02,871 --> 00:02:07,768 I'm gonna comment out the lines from before that show the number of 25 00:02:07,768 --> 00:02:09,351 ties in our samples. 26 00:02:09,351 --> 00:02:14,190 [BLANK_AUDIO] 27 00:02:14,190 --> 00:02:21,782 And then now we're, we're gonna print out some, which was this number. 28 00:02:21,782 --> 00:02:25,911 [SOUND] Gonna clear this. 29 00:02:25,911 --> 00:02:32,100 I'm gonna quickly show you a couple of ways to write a more condensed form. 30 00:02:32,100 --> 00:02:39,330 We're gonna use list comprehension, and so, it'll be a, a more succinct version. 31 00:02:39,330 --> 00:02:47,366 [SOUND] So, we're gonna copy that just to quickly type this up. 32 00:02:47,366 --> 00:02:52,450 [SOUND] And we're gonna remove this line, 33 00:02:52,450 --> 00:02:56,087 cuz we don't need the total. 34 00:02:56,087 --> 00:03:00,547 Instead of this loop, what we're gonna 35 00:03:00,547 --> 00:03:04,908 write here is, for x in data_sample. 36 00:03:04,908 --> 00:03:10,124 [SOUND] And also we're gonna 37 00:03:10,124 --> 00:03:14,891 use the third value of x. 38 00:03:14,891 --> 00:03:16,470 And we're gonna turn that into a float. 39 00:03:19,170 --> 00:03:24,170 So what this really ends up being is instead of the row here. 40 00:03:24,170 --> 00:03:25,590 That's where it is. 41 00:03:26,830 --> 00:03:31,070 And instead of using total equals price what we can do instead is 42 00:03:31,070 --> 00:03:37,720 the price_total is equal to that line. 43 00:03:37,720 --> 00:03:43,340 So it removes these three lines and the return that. 44 00:03:44,610 --> 00:03:45,170 Actually. 45 00:03:47,610 --> 00:03:49,070 We wanna sum up this list. 46 00:03:51,570 --> 00:03:59,639 So, we return the sum of prices. 47 00:03:59,639 --> 00:04:06,962 [SOUND]. 48 00:04:06,962 --> 00:04:08,421 And. 49 00:04:08,421 --> 00:04:15,121 [BLANK_AUDIO] 50 00:04:15,121 --> 00:04:19,350 And that's how we get a more concise version of code. 51 00:04:19,350 --> 00:04:23,130 So I'm actually gonna comment this out very quickly. 52 00:04:23,130 --> 00:04:26,790 So we can move on to the next example. 53 00:04:28,010 --> 00:04:32,923 So I'm gonna show you another 54 00:04:32,923 --> 00:04:37,836 kind of list comprehension, 55 00:04:37,836 --> 00:04:42,159 [SOUND] and this time we're 56 00:04:42,159 --> 00:04:47,081 gonna use a lambda function. 57 00:04:47,081 --> 00:04:52,080 So once again prices equals a one liner which is a list. 58 00:04:52,080 --> 00:04:54,770 We're gonna map over the data sample. 59 00:05:00,550 --> 00:05:06,590 And what we want to do here is use lambda x, so the lambda function. 60 00:05:08,650 --> 00:05:15,370 And we're gonna get the float type of the x2. 61 00:05:15,370 --> 00:05:17,550 So, of each row basically. 62 00:05:18,570 --> 00:05:25,950 And again, we're just gonna return the sum and that'll give us. 63 00:05:25,950 --> 00:05:27,120 The exact same number. 64 00:05:27,120 --> 00:05:32,085 So, we can print that out. 65 00:05:32,085 --> 00:05:34,752 [SOUND] 66 00:05:34,752 --> 00:05:40,621 There we go. 67 00:05:40,621 --> 00:05:43,820 So, now I'm gonna commented out again. 68 00:05:43,820 --> 00:05:49,360 And now I can show you NumPy's sum function. 69 00:05:49,360 --> 00:05:52,530 With NumPy you can use numpy.sum and 70 00:05:52,530 --> 00:05:55,900 it's very similar to this Python's built in sum. 71 00:05:55,900 --> 00:05:57,833 [SOUND]. 72 00:05:57,833 --> 00:06:07,833 [BLANK_AUDIO] 73 00:06:18,045 --> 00:06:24,266 We're gonna be super certain that our prices is float type so 74 00:06:24,266 --> 00:06:29,820 we're gonna convert the whole list of prices into float. 75 00:06:31,820 --> 00:06:33,410 We're gonna use a list comprehension. 76 00:06:33,410 --> 00:06:39,320 We're gonna say float each line for line in price. 77 00:06:39,320 --> 00:06:47,495 And the total will be equals to numpy.sum and 78 00:06:47,495 --> 00:06:53,362 prices_in_float. 79 00:06:53,362 --> 00:06:59,874 [SOUND] And, what price is equal to here, 80 00:06:59,874 --> 00:07:04,471 price is equal to my_csv and 81 00:07:04,471 --> 00:07:08,503 the column price label. 82 00:07:08,503 --> 00:07:15,003 [SOUND] We're gonna print this out. 83 00:07:15,003 --> 00:07:22,610 [SOUND] We're gonna print out the my sum and 84 00:07:22,610 --> 00:07:29,563 we'll see that the value is the same, 85 00:07:29,563 --> 00:07:35,880 but they've truncated it for us.