Search code examples
pythonpandasdataframecalculated-columns

How can I create column with row value and column names based on condition > 0


I have the following data frame:

F1 F2 F3
1 0 0
1 0 1
0 1 0
0 1 1

And I need a new column with this result:

F1 F2 F3 Type
1 0 0 1F1
1 0 1 1F1 1F3
0 1 0 1F2
0 1 1 1F2 1F3

Basically the new column needs to contain the value + the name of that column if the value is higher than 0.

I've tried all sorts of iterrows and iteritems but couldn't execute the code properly.

Thanks for the help.


Solution

  • Try with stack , then groupby agg back

    s = df.stack().reset_index(level=1)
    s = s[s[0]!=0]
    df['new'] = (s[0].astype(str)+s['level_1']).groupby(level=0).agg(' '.join)
    df
       F1  F2  F3      new
    0   1   0   0      1F1
    1   1   0   1  1F1 1F3
    2   0   1   0      1F2
    3   0   1   1  1F2 1F3