1
votes

How can I retrieve the columns in which at least once appears a value < threshold?

For instance:

THRESHOLD = 0

print(df)

Col_1  Col_2  Col_3   Col_4
   1     3      5      -9
   1     3      5      -9
   1    -2      5      -9

print(final_df)

  Col_2    Col_4
     3      -9
     3      -9
    -2      -9

I tried with:

df[(df < 0).any(1)]  

But it reports the rows, not the columns, in which at least one element < 0 appears.

2
you can transpose but it's probably not the best solution. df.T[(df.T < 0).any(1)].T - gyx-hh

2 Answers

1
votes

You can issue df.loc[:, (df < 0).any(0)].

>>> df                                                                                                                       
   Col_1  Col_2  Col_3  Col_4
0      1      3      5     -9
1      1      3      5     -9
2      1     -2      5     -9
>>>
>>> df.loc[:, (df < 0).any(0)] 
   Col_2  Col_4
0      3     -9
1      3     -9
2     -2     -9

Details:

(df < 0).any(0) will give you the columns that have a value lower than zero, because any(0) operates along the rows.

>>> df < 0                                                                                                                    
   Col_1  Col_2  Col_3  Col_4
0  False  False  False   True
1  False  False  False   True
2  False   True  False   True
>>>
>>> (df < 0).any(0)                                                                                                            
Col_1    False
Col_2     True
Col_3    False
Col_4     True
dtype: bool

Then df.loc[:, (df < 0).any(0)] selects all rows and the columns for which df < 0).any(0) is True by boolean indexing.

1
votes

Using axis=0 with .loc

df.loc[:,(df < 0).any(0)]
Out[215]: 
   Col_2  Col_4
0      3     -9
1      3     -9
2     -2     -9

Or we using .iloc with nonzero

df.iloc[:,(df<0).any().nonzero()[0]]
Out[230]: 
   Col_2  Col_4
0      3     -9
1      3     -9
2     -2     -9