Locked lesson.
About this lesson
In this video, we'll explain how to select data based on multiple conditional statements.
Exercise files
Download this lesson’s related exercise files.
Multiple Conditions.docx57.2 KB Multiple Conditions - Solution.docx
55.5 KB
Quick reference
Multiple Conditions
You can search using multiple conditionals using And (&) and Or (|) operators.
When to use
Use these when you want to search multiple conditionals.
Instructions
When you want to search more than one condition and want them all to be true:
my_df[(my_df["Total"] < 0) & (my_df["Wed"] < -1)]
When you want to search more than one condition and want either of them to be true:
my_df[(my_df["Total"] < 0) | (my_df["Wed"] < -1)]
Hints & tips
- AND: my_df[(my_df["Total"] < 0) & (my_df["Wed"] < -1)]
- OR: my_df[(my_df["Total"] < 0) | (my_df["Wed"] < -1)]
- 00:05 Okay, in the last video we looked at how to select data based on conditional
- 00:08 statements.
- 00:09 In this video, we want to look at multiple conditional statements.
- 00:12 So more than one condition.
- 00:15 So if you were confused in the last video,
- 00:17 you're going to have your mind blown in this one.
- 00:19 Okay, so here's our conditional from our last video.
- 00:21 So we're just looking for any rows with totals less than 0.
- 00:26 And when we run this, we get two rows.
- 00:29 Because in our original data frame here,
- 00:31 these are the only two rows that have less than 0 things.
- 00:35 Well, what if we wanted to search for more than just that one conditional.
- 00:39 How would we do that.
- 00:40 Well, we can use and and or.
- 00:42 And to do that, all we have to do is wrap our conditional statement in parentheses,
- 00:48 and then use, and or or, and then just create another conditional statement.
- 00:52 Now, this won't exactly work, we're going to get an error if we do this, and
- 00:56 I'll show you why in just a second.
- 00:57 But this is basically the logic behind it.
- 00:59 So let's create a second conditional statement.
- 01:02 I'm just going to copy this whole thing here and paste it in.
- 01:07 So we want totals less than 0, and
- 01:11 let's search for Wednesday's less than -1.
- 01:16 So, this is -0.2, that's not less than -1.
- 01:22 This is -1.9, that is less than -1.
- 01:26 So we should just get this row, C.
- 01:30 So let's shift enter to run this.
- 01:32 And we get that error that I was talking about,
- 01:34 truth value of a series is ambiguous.
- 01:36 And that's because we can't actually use the word and.
- 01:40 Now, with Python, regular Python,
- 01:42 when you're doing multiple conditional statements,
- 01:44 you could just use the word and, and it works, but it doesn't for pandas.
- 01:47 And the reason that is is because we're talking about lots of
- 01:50 different numbers here.
- 01:52 And pandas has a hard time evaluating lots of different numbers with the and
- 01:56 function, right.
- 01:57 So instead of using and, we have to use the ampersand.
- 02:01 It's just this guy right here.
- 02:02 And that's usually shift seven on most keyboards.
- 02:05 And a lot of programming languages, they use either and or the ampersand,
- 02:10 or in some cases, they use both.
- 02:11 So this is not such a weird thing.
- 02:13 It's just what pandas requires.
- 02:15 So now if we go ahead and run this,
- 02:17 we get in fact C, which is the column we thought we would get.
- 02:20 Because we're here, our total is less than 0.
- 02:23 Well, here total is less than 0, and Wednesday is less than -1.
- 02:30 Wednesday is less than -1.
- 02:31 No other rows in our data frame meet this condition, right.
- 02:36 So in order for this to evaluate true, this and this both have to be true.
- 02:44 If either one of those isn't true, the whole thing won't evaluate into true, and
- 02:48 that's why none of these other rows got selected, because it wasn't true for
- 02:51 those rows.
- 02:52 So that's and.
- 02:54 Now we can also use or, o r.
- 02:57 But just like with and, if we try and run this with or,
- 02:59 we're going to get that same truth value series is ambiguous error.
- 03:03 And so for or, we need to use the symbol for or, which is the pipe,
- 03:07 which is that straight bar, up and down, right.
- 03:10 So if we run this, we get a bunch of results, A, B, and C because here,
- 03:16 either one of these has to be true in order for this to evaluate true, right.
- 03:21 So this has to be true or this has to be true.
- 03:26 One or the other, or they can both be.
- 03:29 So let's look at this.
- 03:30 Total's less than 0.
- 03:32 Well, that's this one, this one, and this one, B, C, and D, right.
- 03:37 Well, D's not here, and A is here.
- 03:39 So what's going on.
- 03:40 Well, let's keep looking at this.
- 03:42 We also want or, so let's see.
- 03:46 So B and C are both negative.
- 03:48 So B and C shows up, but A is positive.
- 03:53 Well, we also want to search for Wednesday less than -1.
- 03:57 So, for A, it's Wednesday is less than -1 just barely, by 0.04, right.
- 04:04 So A gets put on here, because or evaluated true for A.
- 04:09 Even though this was false, this was true.
- 04:13 And since only one of these two have to be true, A gets added.
- 04:16 So those are multiple conditionals.
- 04:19 So just remember you can't use the words and and or,
- 04:22 you have to use the ampersand or the pipe symbol.
- 04:25 And other than that, it's pretty simple.
- 04:28 You just hash your conditional statements in your parentheses and you're good to go.
- 04:32 So, in the next video, we'll go ahead and look at changing indexes.
Lesson notes are only available for subscribers.