I have a dataframe that looks like:
body label
the sky is blue [noun]
the apple is red. [noun]
Let's take a walk [verb]
I want to add an item to the list in label depending on if there is a color in the body column.
Desired Output:
body label
the sky is blue [noun, color]
the apple is red. [noun, color]
Let's take a walk [verb]
I have tried:
data.loc[data.body.str.contains("red|blue"), 'label'] = data.label.str.append('color')
CodePudding user response:
One option is to use apply
on the Series and then directly append to list:
data.loc[data.body.str.contains('red|blue'), 'label'].apply(lambda lst: lst.append('color'))
data
body label
0 the sky is blue [noun, color]
1 the apple is red. [noun, color]
2 Let's take a walk [verb]