0

As you know, basic indexing in Pandas is really easy.

import pandas as pd
df = pd.DataFrame({'a':[1,2,3,4]})

Let's say I want every row where 'a' is greater than 2.

df[df['a'] > 2]

Ok, but now how do I use multiple conditionals? Let's try and find every row where 'a' is in range(2,4).

df[df['a'] in range(2,4)]
df[1 < df['a'] < 4]
df[df['a'] >1 and df['a'] <4]

All of these lines return the error message:

"The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

But as far as I can tell, these methods don't do what I'm attempting.

EDIT: Found the answer right after posting.

df[df.a > 1][df.a < 4]

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Gavin Ray
  • 129
  • 1
  • 7
  • 1
    It's great that you found a solution. However note that the solutions from @Grayrigel are more efficient, especially the one using between(). – ypnos Oct 28 '20 at 07:52

1 Answers1

1

You can separate multiple conditions in the parenthesis separating them & (and) or | (or). Here is an example:

df[(df.a > 1) & (df.a < 4)]

Alternatively, in this case, you can also use gt(greater than) or lt (less than) like this:

df[df.a.gt(1) & df.a.lt(4)]

Or you can use the between method:

df[df.a.between(1,4, inclusive=False)]

Output:

   a
1  2
2  3
Grayrigel
  • 3,474
  • 5
  • 14
  • 32