Ali
Ali

Reputation: 466

How to select rows with only positive or negative values in Pandas

I have the following DF:

df=pd.DataFrame({
                 'x':[1, 2, 3, -1, -2, -3],
                 'y':[-1, 3, 2, -4, 3, -2],
                 'z':[1 , 1, 5, 2, 1, -1]

}) or

       x  y  z
    0  1 -1  1
    1  2  3  1
    2  3  2  5
    3 -1 -4  2
    4 -2  3  1
    5 -3 -2 -1

The goal is to find the rows that all of their elements have the same (either negative or positive) values. In this example, it means selecting rows 1, 2, and 5.

I would appreciate any help. I am aware of this question: Pandas - Compare positive/negative values but it doesn't address the case where the values are negative.


Thank you :)

Upvotes: 10

Views: 26760

Answers (2)

Raouf Mamedov
Raouf Mamedov

Reputation: 11

I would do that with numpy:

import numpy as np

df[df.columns[df.apply(np.sign).nunique()==1]].T

Upvotes: 1

Andrej Kesely
Andrej Kesely

Reputation: 195438

You can use the OR (|) to combine conditions:

print(df[(df[df.columns] >= 0).all(axis=1) | (df[df.columns] <= 0).all(axis=1)])

Prints:

   x  y  z
1  2  3  1
2  3  2  5
5 -3 -2 -1

Or simpler:

print(df[(df >= 0).all(axis=1) | (df <= 0).all(axis=1)])

EDIT: As @Erfan stated in the comments, if you want strictly negative (without 0), you use <:

print(df[(df >= 0).all(axis=1) | (df < 0).all(axis=1)])

Upvotes: 12

Related Questions