Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove rows in python less than a certain value

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. Thanks in advance!

like image 329
JAG2024 Avatar asked Jan 23 '17 06:01

JAG2024


People also ask

How do you delete a row based on a condition in python?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do you drop rows with certain values?

One of the fastest ways to delete rows that contain a specific value or fulfill a given condition is to filter these. Once you have the filtered data, you can delete all these rows (while the remaining rows remain intact).

How do you delete multiple rows in Python?

Delete rows and columns from a DataFrame using Pandas drop() Delete one or many rows/columns from a Pandas DataFrame can be achieved in multiple ways. Among them, the most common one is the drop() method.


3 Answers

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]  
like image 77
Jay Parikh Avatar answered Oct 22 '22 14:10

Jay Parikh


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)]
like image 38
piRSquared Avatar answered Oct 22 '22 15:10

piRSquared


I have another suggestion, which could help

df3 = result.drop(result[result['Value'] < 10].index, inplace = True)
like image 8
Ashish Tripathi Avatar answered Oct 22 '22 16:10

Ashish Tripathi