I have a dataframe in the form of
black orange yellow green
1 0 1 0 1
2 0 0 0 1
3 1 0 0 0
I would like to create another column that would tell which colours are present, so the final output SHOULD be:
black orange yellow green colours
1 0 1 0 1 orange and green
2 0 0 0 1 green
3 1 0 0 0 black
Thanks in advance. P.S. Forgot to add there are a lot more columns than this - plenty of more colours.
CodePudding user response:
Use DataFrame.dot
with the columns names and then remove last 5
values:
df['colours'] = df.dot(df.columns ' and ').str[:-5]
print (df)
black orange yellow green colours
1 0 1 0 1 orange and green
2 0 0 0 1 green
3 1 0 0 0 black
CodePudding user response:
Or use apply
:
>>> df['colours'] = df.apply(lambda x: ' and '.join(x[x == 1].index), axis=1)
>>> df
black orange yellow green colours
1 0 1 0 1 orange and green
2 0 0 0 1 green
3 1 0 0 0 black
>>>