2

I have a dataframe similar to this:

       Male  Over18  Single
0     0       0       1
1     1       1       1
2     0       0       1

I would like an extra column which gets a commaseperated string with the columnnames where the value is 1:

   Male  Over18  Single        CombinedString
0     0       0       1                Single
1     1       1       1  Male, Over18, Single
2     0       0       1                Single

Hope there is someone out there who can help :)

2 Answers 2

7

One pandaic way is to perform a pandas dot product with the column headers:

df['CombinedString'] = df.dot(df.columns+',').str.rstrip(',')
df

   Male  Over18  Single      CombinedString
0     0       0       1              Single
1     1       1       1  Male,Over18,Single
2     0       0       1              Single
Sign up to request clarification or add additional context in comments.

Comments

1

Another method would be to use .stack() and groupby.agg()

df['CombinedString'] = df.mask(df.eq(0)).stack().reset_index(1)\
                     .groupby(level=0)['level_1'].agg(','.join)


print(df)

   Male  Over18  Single      CombinedString
0     0       0       1              Single
1     1       1       1  Male,Over18,Single
2     0       0       1              Single

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.