44

I feel like this question must have been answered by someone before, but I can't find an answer on stack overflow!

I have a dataframe result that looks like this and I want to remove all the values less than or equal to 10

>>> result
                       Name              Value      Date
189                   Sall                19.0  11/14/15
191                     Sam               10.0  11/14/15
192                 Richard               21.0  11/14/15
193                  Ingrid                4.0  11/14/15 

This command works and removes all the values that are 10:

df2 = result[result['Value'] != 10]

But when I try to add the <= qualifier I get the error message SyntaxError: invalid syntax

df3 = result[result['Value'] ! <= 10]  

I feel like there is probably a really simple solution.

starball
  • 20,030
  • 7
  • 43
  • 238
JAG2024
  • 3,987
  • 7
  • 29
  • 58

4 Answers4

64

Instead of this

df3 = result[result['Value'] ! <= 10]  

Use

df3 = result[~(result['Value'] <= 10)]  

It will work. OR simply use

df3 = result[result['Value'] > 10]  
Jay Parikh
  • 2,419
  • 17
  • 13
14

python doesn't use ! to negate. It uses not. See this answer
In this particular example != is a two character string that means not equal. It is not the negation of ==.

option 1
This should work unless you have NaN

result[result['Value'] > 10]

option 2
use the unary operator ~ to negate a boolean series

result[~(result['Value'] <= 10)]
Community
  • 1
  • 1
piRSquared
  • 285,575
  • 57
  • 475
  • 624
9

I have another suggestion, which could help

df3 = result.drop(result[result['Value'] < 10].index)
Tanmay
  • 13
  • 1
  • 5
Ashish Tripathi
  • 301
  • 3
  • 3
0

There's also df.query():

result.query('Value > 10')
                       Name              Value      Date
189                   Sall                19.0  11/14/15
191                     Sam               10.0  11/14/15
192                 Richard               21.0  11/14/15
Janosh
  • 3,392
  • 2
  • 27
  • 35