I have an input data frame which looks like this:
print (df)
Id A B C D
0 101 0 0 0 1
1 102 0 0 0 0
2 103 1 0 1 0
3 104 1 0 1 1
Output: I want to print the Column Names of the columns which contain '1' in it. The output data frame should look like this. If 1 is not present it should return an empty string.
Id 101- D (4th index) has 1
Id 102- None
Id 104- A, C and D which are 1,3,4 indexes
So, a sample output would look like:
print (df)
Id Result
0 101 D
1 102
2 103 A,C
3 104 A,C,D
I have tried this code but it didn't work:
df['out'] = df.apply(
lambda x: ','.join(str(ele) for ele in [df.column for df.column,df.column.values in enumerate(x[:]) if df.column.values is 1]),
axis=1)
df_out