1

How would I filter a dataframe using 'and' ? In other words, if I have a dataframe named m and it has columns a,b,c,d,e, how would i return all rows where the values in column b are greater than 120 and the vales in column c = 7.3 ?

I tried this but I'm getting an error:

print(m[m['b'] >120, m['c'] ==7.3])

3
  • Try using the standard python or command. Commented Jun 22, 2014 at 0:19
  • print(m[m['b'] >120] or m[m['c'] ==7.4]) ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). What is the right way to do this ? Commented Jun 22, 2014 at 0:31
  • stackoverflow.com/questions/8916302/… Commented Jun 22, 2014 at 0:43

2 Answers 2

2

To expand on GoBrewers14's answer, you need to wrap around parenthesis to overcome the order of evaluation of the operators in python.

For example, the next statement fails:

In [3]: 1 > 0 & 'a' < 'b'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-5d58a7b0bade> in <module>()
----> 1 1 > 0 & 'a' < 'b'

TypeError: unsupported operand type(s) for &: 'int' and 'str'

because python evaluates first 0 & 'a'. That's why you need to wrap the statements with parenthesis to make sense:

In [4]: (1 > 0) & ('a' < 'b')
Out[4]: True

In short, you are looking for:

m[(m['b'] > 120) & (m['c'] == 7.3)]
Sign up to request clarification or add additional context in comments.

Comments

0
>>> mask = (m.b > 120) & (m.c == 7.3)
>>> m[mask]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.