简体   繁体   中英

How to check if any row is more than x and return name of column?

I have dataframe of id, col1,col2,col3,col4 as it is described on picture.

I want to write function which find if any row is more than x an return name of column in which this condition was true and return it in result column.

userid  col1    col2    col3    col4    result
d1  40  50  75  65  col3
d2  54  20  61  71  col4
d3  12  75  12  60  col2
d4  75  12  14  16  col1

See Image

You can use idxmax with (axis=1) to work on columns :

>>> df[['col1','col2','col3','col4']].idxmax(axis=1)
0    col3
1    col4
2    col2
3    col1
dtype: object

And to assign it to your df :

>>> df['result'] = df[['col1','col2','col3','col4']].idxmax(axis=1)

You get :

>>> df
    userid  col1    col2    col3    col4    result
0   d1        40      50      75      65      col3
1   d2        54      20      61      71      col4
2   d3        12      75      12      60      col2
3   d4        75      12      14      16      col1

You can filter those columns with numbers using .select_dtypes() and then compare those columns with x and get the column name by .idxmax(axis=1) across the row, as follows:

(Let's assume x = 70 below):

x = 70           
df2 = df.select_dtypes('number')
df['result'] = (df2 > x).idxmax(axis=1)

Result:

print(df)

  userid  col1  col2  col3  col4 result
0     d1    40    50    75    65   col3
1     d2    54    20    61    71   col4
2     d3    12    75    12    60   col2
3     d4    75    12    14    16   col1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM