Locked lesson.
About this lesson
In this video, we'll discuss two different ways to select a row within a data frame, including the loc function.
Exercise files
Download this lesson’s related exercise files.
Selecting Rows.docx57 KB Selecting Rows - Solution.docx
55.4 KB
Quick reference
Selecting Rows
There are two main ways to grab Rows in a DataFrame: loc() and iloc().
When to use
Use either of these methods to grab the data from a row in a DataFrame.
Instructions
To grab a row named "Bob" in a DataFrame named my_df:
my_df.loc["Bob"]
To grab a row by its index, assuming Bob is the first row in the DataFrame:
my_df.iloc[0]
Hints & tips
- my_df.loc["Bob"]
- my_df.iloc[0]
- 00:05 Hi, in this video I want to talk about selecting rows.
- 00:08 And there's two main methods to select rows, and
- 00:10 we're going to look at both of them in this video.
- 00:12 So, here we've got our data frame, and we got our A, B, C and D rows.
- 00:18 So let's say we want to grab one of these rows and do stuff with the data.
- 00:22 How do we grab a row?
- 00:23 So let's say we want to grab B, right?
- 00:26 Well, to do that we use the location function and
- 00:29 we could just call my_df.loc, stands for location.
- 00:34 Now with most functions we're going to use parentheses and
- 00:36 then pass in some arguments.
- 00:38 With the location function for some reason we use square brackets.
- 00:41 So that's kind of a weird thing, I just kind of go with it right?
- 00:45 So, what do we want to grab?
- 00:46 We want to grab row B, so we could just pass in row B.
- 00:50 And when we do that it looks like, hey look at that,
- 00:53 that's our old friend the series back again.
- 00:56 And that's kind of cool.
- 00:57 Now we think this is a series, but we're not quite sure.
- 01:00 So we can always confirm that to ourselves by running a type function.
- 01:04 So we just copy this whole thing, just to make sure,
- 01:07 because we always want to know what kind of data we're dealing with.
- 01:10 And when we run this, we see, sure enough, this is a panda's series.
- 01:13 So that's cool.
- 01:15 So again, here we see our name and the data type is afloat of 64.
- 01:19 And really, that's all there is to grabbing rows.
- 01:23 That's sort of the first way to do it.
- 01:25 And generally speaking, that's probably the way you're always going to use because
- 01:29 it's really easy just to grab your label.
- 01:32 This is a B, so we call location on B.
- 01:35 But there's actually another method you can use using index numbers,
- 01:40 index locations.
- 01:41 Remember when we talked about these earlier, these are index labels,
- 01:46 right, A, B, C and D.
- 01:48 Well, an index if you think of like a Python list,
- 01:51 they're usually numbered indexes.
- 01:54 And Python lists start at 0, so 0, 1, 2, 3.
- 01:57 Here we would have A would be 0, B would be 1, C would be 2 and D would be 3.
- 02:03 So we can access those numbers, if for some reason,
- 02:06 we don't want to access the actual label itself.
- 02:09 So to do that we use the iloc.
- 02:12 It stands for index location, right?
- 02:16 So instead of B here, if we wanted to call B it would be 0 1.
- 02:21 So B is the first, even though it's listed as the second index number.
- 02:27 So if we run this shift enter we get the same thing.
- 02:29 So let's grab something else.
- 02:31 Let's say we want D.
- 02:33 That would be 0, 1, 2, 3.
- 02:36 So we would just call iloc 3.
- 02:40 And if we shift enter to run this, it looks like here we have -1.004,
- 02:47 -1.004, 0.758, 0.758,
- 02:51 1.89, 1.89 and 1.649, 1.649.
- 02:57 So, strictly speaking, are you ever going to use the iloc function when
- 03:01 you can just use the regular location function?
- 03:05 Probably not, I mean, it's so much easier just to grab row A, right?
- 03:10 You don't have to think about it,
- 03:12 you don't have to remember that, these start at 0 instead of 1, right?
- 03:18 It's probably easier.
- 03:19 So you're, generally speaking, probably going to just use the location 1.
- 03:22 And if we want to grab C, just do it like that and boom.
- 03:26 But either way, it's pretty simple.
- 03:28 So in the next video, we'll go ahead and
- 03:30 look at selecting subsets of both rows and columns.
- 03:34 And that will be in the next video.
Lesson notes are only available for subscribers.