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
                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
                        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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With