Zanshin
Zanshin

Reputation: 1272

Select column with only one negative value

I'd like to select the columns that only have one negative value or none. How can I construct this looking at this example? I've been searching for something similar, didn't succeed though. Thanks for any help.

N = 5
np.random.seed(0)

df1 = pd.DataFrame(
          {'X':np.random.uniform(-3,3,N),
           'Y':np.random.uniform(-3,3,N),
           'Z':np.random.uniform(-3,3,N),
                })

          X         Y         Z
0  0.292881  0.875365  1.750350
1  1.291136 -0.374477  0.173370
2  0.616580  2.350638  0.408267
3  0.269299  2.781977  2.553580
4 -0.458071 -0.699351 -2.573784

So in this example I'd like to return columns X and Z.

Upvotes: 0

Views: 199

Answers (3)

BENY
BENY

Reputation: 323226

Or You can try :

df1.columns[(df1<0).sum(0).lt(1)]
Out[338]: Index(['X', 'Z'], dtype='object')

Upvotes: 0

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

You can use iloc to do that i.e

df1.iloc[:,((df1<0).sum(0) <= 1).values]

or (Thanks Jon)

df1.loc[:,df1.lt(0).sum() <= 1]

Output:

          X         Z
0  0.292881  1.750350
1  1.291136  0.173370
2  0.616580  0.408267
3  0.269299  2.553580
4 -0.458071 -2.573784

Upvotes: 2

Divakar
Divakar

Reputation: 221554

Use np.sign to get the signs. Look for negative signs. Get the count of negative numbers for each column. Compare against the threshold of 1 to get a mask. Select the column names from the mask.

Hence, the implementation -

df1.columns[(np.sign(df1)<0).sum(0)<=1].tolist()

Or directly compare against 0 to replace the use of np.sign -

df1.columns[(df1<0).sum(0)<=1].tolist()

This gives us the column names. To select entire columns, other solutions I think have covered it.

Upvotes: 3

Related Questions