Locked lesson.
About this lesson
Let's discuss how to select specific points within your data frames, such as the intersection of a row and column, or multiple points within different rows and columns.
Exercise files
Download this lesson’s related exercise files.
Selecting Subsets of Rows and Columns.docx57 KB Selecting Subsets of Rows and Columns - Solution.docx
55.4 KB
Quick reference
Selecting Subsets of Rows and Columns
We can select specific data points in a DataFrame as well as specific Subsets.
When to use
Use these methods whenever you want to select a certain point in a dataframe, or a subset of points.
Instructions
To grab a specific point in a DataFrame named my_df, use the loc() function:
my_df.loc["ROW", "COLUMN"]
To grab a subset of points in a DataFrame named my_df:
my_df.loc[["ROW","ROW"], ["COLUMN", "COLUMN"]]
Hints & tips
- my_df.loc["ROW", "COLUMN"]
- my_df.loc[["ROW","ROW"], ["COLUMN", "COLUMN"]]
- 00:05 Okay, in this video, I want to talk about selecting specific points and
- 00:09 also selecting subsets of rows and columns.
- 00:13 So what do I mean by specific points?
- 00:16 Well, for instance, if we want this thing right here, so if we want B Wednesday,
- 00:21 this -0.201, how can we grab that specific thing?
- 00:26 And by subsets, I mean any sort of subset, so
- 00:30 say we want Monday from A and Wednesday from A, and
- 00:34 Monday from D and Wednesday from D, how do we grab that subset?
- 00:39 So those two things are what we're going to talk about in this video.
- 00:42 So to grab a specific point, we can go ahead and go my_df.loc,
- 00:47 like we did in the last video.
- 00:49 But instead of just passing one row, we need a row and a column.
- 00:57 You can use double or single quotation marks, so
- 01:01 we need a row and a column, all right?
- 01:04 So for row, we want B, so we could change this to B.
- 01:08 And for column, we want Wednesdays, so we change this to Wednesday.
- 01:13 I just wanted to show you the format so it's not confusing, and sure enough,
- 01:18 we get -0.2012 blah, blah, blah, 0.201217, you notice this one's much larger,
- 01:25 it just rounded on this one and here we get the whole thing.
- 01:29 So what exactly is this data that just got passed to us?
- 01:33 Well, let's run a type and see, this is kind of interesting.
- 01:36 It's a numpy.float64,
- 01:39 floats are decimals long decimals, but they're kind of interesting, it's a NumPy.
- 01:44 NumPy has half back up, remember from the very beginning of the course, Pandas is
- 01:49 built on NumPy, so we see these NumPy things popping up, so that's pretty cool.
- 01:55 So that's how to grab a specific point, fairly simple.
- 01:58 To grab a subset is very sort of similar,
- 02:03 let's just go my_df.loc again.
- 02:07 And before we did a row column,
- 02:10 we're going to do the same thing, but instead of passing just one point,
- 02:13 we're going to pass a list with several points for both of these.
- 02:18 So for our row, say we want row A and row D, so we could just pass A,
- 02:23 D, and for our columns, say we want Monday,
- 02:31 And Wednesday.
- 02:34 So if we run this, boom, we get Monday and Wednesday for A and D.
- 02:38 And we can confirm, we can go 0.468 and that's 0.468, -1.046,
- 02:45 -1.046, and the same thing for D, -1004, there we go.
- 02:50 And Wednesday, it's 1.894, 1.894, so very cool and pretty simple.
- 02:56 Now, we can do more than one or more than two, we can do A, D, and C.
- 03:00 Now, if we run this, check it out, we get A, D, and C.
- 03:05 But it's in the exact order that we put here,
- 03:09 not in the order that it's listed here.
- 03:11 So here it's A, B, C, D, so you would think it should be A, B, C, D, or
- 03:16 at least A, C, D.
- 03:17 But it's not, it's designated just by how we put it in here.
- 03:20 So if we wanted it in the original order,
- 03:22 we would have to put it like that inside of here, and then we get A, C, and D.
- 03:27 Same thing here, we can add as many of these as we want, if we want the total
- 03:32 two, we can run this and notice I mixed quotation marks.
- 03:37 So these are double quotes and these are single quotes, you can do both like this.
- 03:41 You can do all single quotes, you can do all double quotes,
- 03:44 it really doesn't matter.
- 03:46 That's how to grab both specific points from our data frame and subsections.
- 03:53 In the next video, we'll look at conditional selection,
- 03:56 making selections based on certain conditions.
Lesson notes are only available for subscribers.