Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the column name resulting True in Pandas boolean reduction

I have a df,

     0               1              2          A
-0.740485792    -0.299824912    0.169113705    1
 1.120120949    -0.62580736     0.013757667    2
-0.685112999     0.439492717    -0.484524907   3

I am trying get the column name which has all the values greater than 0,

I tried (df > 0).all()

Out[47]: 
 0    False
 1    False
 2    False
 A     True
 dtype: bool

How to get only the column name which are True,

My expected output is "A", thanks in advance.

Question 2 on sort_index()

 df2 = pd.DataFrame({"A":[3,2,1]}, index=[2,1,0])

 Out[395]:
    A
2   3
1   2
0   1

df2.sort_index(axis=1)

    A
2   3
1   2
0   1

expected output is,

    A
0   3
1   2
2   1
like image 701
Vicky Avatar asked Oct 28 '25 04:10

Vicky


1 Answers

Use boolean indexing with df.columns:

c = df.columns[(df > 0).all()]
print (c)
Index(['A'], dtype='object')
like image 194
jezrael Avatar answered Oct 29 '25 19:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!