0

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') 

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

2 Comments

this works. thanks!
@mpatil Cool. Glad to hear!

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.