1

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

1 Answer 1

3

Use DataFrame.dot with all columns without first for matrix multiplication, add separator and last remove last character (separator) by indexing:

df['Result'] = df.iloc[:, 1:].dot(df.columns[1:] + ',').str[:-1]
print (df)
    Id  A  B  C  D Result
0  101  0  0  0  1      D
1  102  0  0  0  0       
2  103  1  0  1  0    A,C
3  104  1  0  1  1  A,C,D

Your solution should be changed with extract matched index with join:

df['Result'] = df.apply(lambda x: ','.join(x.index[1:][x.iloc[1:] == 1]), axis=1)
print (df)
    Id  A  B  C  D Result
0  101  0  0  0  1      D
1  102  0  0  0  0       
2  103  1  0  1  0    A,C
3  104  1  0  1  1  A,C,D
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.