Locked lesson.
About this lesson
Selecting data based on conditionals - such as greater than, less than, equal to, not equal to - is an essential part of data analysis. We'll discuss that in this video.
Exercise files
Download this lesson’s related exercise files.
Conditional Selection of DataFrames.docx57.2 KB Conditional Selection of DataFrames - Solution.docx
55.5 KB
Quick reference
Conditional Selection of DataFrames
Conditional Selection allows us to select things from our DataFrame based on certain conditions.
When to use
Use this whenever you need to make a conditional selection within your DataFrame.
Instructions
We can use python conditionals to select data from a DataFrame:
>
<
>=
<=
!=
To return a list of Booleans less than 0:
my_df < 0
To return a dataframe of data less than 0
my_df[my_df < 0]
To return a specific Column less than 0:
my_df["Total"] < 0
To return rowns less than Zero in a specific column:
my_df[my_df["Total"] < 0 ]
To return rows just from a specific column less than Zero:
my_df[my_df["Total"] < 0 ]["Total"]
To return more than one column, less than zero:
my_df[my_df["Total"] < 0 ][["Mon", "Total"]]
Hints & tips
- Booleans: my_df < 0
- my_df[my_df < 0]
- my_df["Total"] < 0
- my_df[my_df["Total"] < 0 ]
- my_df[my_df["Total"] < 0 ]["Total"]
- my_df[my_df["Total"] < 0 ][["Mon", "Total"]]
- 00:05 So in this video, we want to look at conditional selection.
- 00:07 So think of conditionals as greater than, less than, equal to,
- 00:12 greater than or equal to, not equal to, so some condition.
- 00:15 So we want to select things based on those conditions.
- 00:20 So let's just start out by saying we're looking through here and
- 00:23 we've got some data that has negative and positives.
- 00:29 So let's grab all the negative data.
- 00:31 So we could start out just by going my_df and then just saying less than 0.
- 00:37 And when we do that, we get this weird kind of table with all these true falses.
- 00:41 Now, these are Booleans, Boolean means true or false.
- 00:44 So we can see here the number is positive,
- 00:47 so that's false, it's false, it's not less than 0, positive.
- 00:53 Here's false, here is true,
- 00:54 this is a negative, it's true if this is a negative number, right?
- 00:59 So we can also flip this around and go greater than, right?
- 01:03 So it's true that this is greater than,
- 01:06 it's false that this is greater than 0, it's less than 0, obviously.
- 01:10 So let me put this back real quick.
- 01:12 So okay, that's kind of interesting,
- 01:13 but it's not probably exactly what we want to do, we can now actually pass
- 01:19 this whole thing into a new data frame to get some different sort of result.
- 01:22 So let's go my_df and just pass all these stuff in.
- 01:28 Now, we get a table that has the actual data itself.
- 01:32 So we want things less than 0.
- 01:35 If a thing is not less than 0, if it's positive, we get a no value.
- 01:39 If it is less than 0, we get the actual number, right?
- 01:42 So this is interesting, it's pretty cool.
- 01:44 We can do this, we can run conditionals based on columns.
- 01:49 So for instance, if we wanted to go my_df and then just grab our Wednesday column,
- 01:55 or let's grab our total column that looks like an interesting column,
- 01:58 we get a series, and there's some positives and negatives.
- 02:01 If we wanted to grab the negatives out of here,
- 02:03 we could just go less than 0 again, and we get that same false, true, true,
- 02:08 false, positive, negative, negative, positive, right?
- 02:12 Again, we can wrap this whole thing in a data frame.
- 02:15 So we go my_df, and then just pass all this stuff in again.
- 02:21 And now, we get some rows, and what's going on here?
- 02:24 Well, we're saying, if the total column has a number less than 0,
- 02:30 negative, then grab it.
- 02:32 Well, that looks like row B and
- 02:34 a row C are the only ones that have negative numbers in their total column.
- 02:40 So we've got row B and row C.
- 02:44 So we can break this down even further if we just want only the total column, right?
- 02:50 We can do that, and now we get a series with B and
- 02:53 C because B and C are the only ones with negatives.
- 02:56 And here we have 0.0081, 0.008, and
- 03:01 here we have -1.2466, -1.2466.
- 03:07 So it's just grabbing these two things from that.
- 03:11 If we want to grab multiple things here, we can pass in two of these,
- 03:15 we could go let's say we want Monday and total.
- 03:19 So we go to our Monday and total, and around this, boom,
- 03:25 we get the negatives from Monday which are B and C and total B and C.
- 03:32 So lots of ways you can do conditionals.
- 03:35 And again, you can use all of your conditional statements.
- 03:38 So that's less than, greater than, less than or equal to,
- 03:44 greater than or equal to, and let's see, not equal to, right?
- 03:51 That's a neat one, and that's how you do that.
- 03:53 So any of your conditional operators from regular Python will work there.
- 03:58 So that's how you do conditional selection, a little bit tricky, but
- 04:01 not too bad.
- 04:02 In the next video, we'll look at multiple conditional selection.
Lesson notes are only available for subscribers.