Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the column name in pandas based on row values? [duplicate]

I have a dataframe, df:

        id_0         id_1          id_2
0         1            0             1
1         1            0             0
2         0            1             0
3         1            1             0
4         0            0             1
5         0            0             0

I want to get the column name for each row if there is 1. How to do that? Thank you.

Result:

           result
0         id_0, id_2
1         id_0
2         id_1
3         id_0, id_1
4         id_2
5         NaN
like image 720
Chan Avatar asked Dec 05 '22 11:12

Chan


2 Answers

Using dot

df.dot(df.columns+',').str[:-1]
Out[168]: 
0    id_0,id_2
1         id_0
2         id_1
3    id_0,id_1
4         id_2
5             
dtype: object
like image 141
BENY Avatar answered May 17 '23 23:05

BENY


Let's try np.argwhere + groupby.

v = np.argwhere(df.values).T
(pd.DataFrame(
       df.columns[v[1]], index=df.index[v[0]], columns=['result']
   ).groupby(level=0).agg(','.join).reindex(df.index)
)

      result
0  id_0,id_2
1       id_0
2       id_1
3  id_0,id_1
4       id_2
5        NaN
like image 40
cs95 Avatar answered May 17 '23 23:05

cs95