Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter rows of pandas dataframe whose values are lower than 0

Tags:

python

pandas

I have a pandas dataframe like this

df = pd.DataFrame(data=[[21, 1],[32, -4],[-4, 14],[3, 17],[-7,NaN]], columns=['a', 'b'])
df

I want to be able to remove all rows with negative values in a list of columns and conserving rows with NaN.

In my example there is only 2 columns, but I have more in my dataset, so I can't do it one by one.

like image 755
dooms Avatar asked Dec 12 '15 18:12

dooms


People also ask

How do I filter specific rows from a DataFrame Pandas?

Filter Rows by Condition You can use df[df["Courses"] == 'Spark'] to filter rows by a condition in pandas DataFrame. Not that this expression returns a new DataFrame with selected rows.

How do you get values greater than in Pandas?

Pandas DataFrame: ge() function The ge() function returns greater than or equal to of dataframe and other, element-wise. Equivalent to ==, =!, <=, <, >=, > with support to choose axis (rows or columns) and level for comparison. Any single or multiple element data structure, or list-like object.


Video Answer


2 Answers

If you want to apply it to all columns, do df[df > 0] with dropna():

>>> df[df > 0].dropna()
    a   b
0  21   1
3   3  17

If you know what columns to apply it to, then do for only those cols with df[df[cols] > 0]:

>>> cols = ['b']
>>> df[cols] = df[df[cols] > 0][cols]
>>> df.dropna()
    a   b
0  21   1
2  -4  14
3   3  17
like image 198
ComputerFellow Avatar answered Oct 10 '22 14:10

ComputerFellow


I've found you can simplify the answer by just doing this:

>>> cols = ['b']
>>> df = df[df[cols] > 0]

dropna() is not an in-place method, so you have to store the result.

>>> df = df.dropna()
like image 36
rgahan Avatar answered Oct 10 '22 16:10

rgahan