Search code examples
pythondataframemetrics

extract column names from dataframe according to data in python


I have a dataframe looks like this

    a   b   c
a  0.3 0.2 0.9 
b  0.9   1 0.8
c  0.2 0.9 0.5

I want to extract the column name if the corresponding value is greater or equal to 0.9.

The result dataframe looks like this:

a  c 
a  a b 
c  b 

Solution

  • Try this:

    In [29]: df.ge(0.9).apply(lambda x: df.columns[x].tolist(), axis=1)
    Out[29]:
    a       [c]
    b    [a, b]
    c       [b]
    dtype: object