Get column name based on condition in pandas

后端 未结 2 1393
名媛妹妹
名媛妹妹 2021-01-29 14:06

I have a dataframe as below:

I want to get the name of the column if column of a particular row if it contains 1 in the that column.

e.g.

For Ro         


        
2条回答
  •  清歌不尽
    2021-01-29 14:16

    Use DataFrame.dot:

    df1 = df.dot(df.columns)
    

    If there is multiple 1 per row:

    df2 = df.dot(df.columns + ';').str.rstrip(';')
    

提交回复
热议问题