0

I have a dataframe:

                            name   alike
0  I love watermelon and banana.   melon
1  I love watermelon and banana.  banana
2                  I love melon.   melon
3                  I love grape.   grape

Code:

df.loc[df['name'].str.contains('watermelon'), 'alike'] = 'watermelon'
print(df)

Output:

                            name       alike
0  I love watermelon and banana.  watermelon
1  I love watermelon and banana.  watermelon
2                  I love melon.       melon
3                  I love grape.       grape

This is not the result I expected, I only want to change "alike" when "name" contains watermelon and "alike" contains "melon".

I've tried in this way:

df.loc[df['name'].str.contains('watermelon') and df['alike'].str.contains('melon'), 'alike'] = 'watermelon'
print(df)

The error said:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Lara19
  • 615
  • 1
  • 9
  • 20
  • Possible duplicate of [pandas: multiple conditions while indexing data frame - unexpected behavior](https://stackoverflow.com/questions/22591174/pandas-multiple-conditions-while-indexing-data-frame-unexpected-behavior) – ted Jul 19 '19 at 03:47

1 Answers1

1

use the bitwise & operator instead of and:

df.loc[df['name'].str.contains('watermelon') & df['alike'].str.contains('melon'), 'alike'] = 'watermelon'

or use apply(..., axis=1)

df = pd.DataFrame({
    "a": ["hello", "heello", "hello you"], 
    "b": ["haha", "hehe", "haha hehe"], 
    "c": ["yes", "yes", "yes"]
})

print(df)

df["c"] = df.apply(
    lambda row: row["c"] if "hello" in row["a"] and "hehe" in row["b"] else "no",
    axis=1)

print(df)

           a          b    c
0      hello       haha  yes
1     heello       hehe  yes
2  hello you  haha hehe  yes
           a          b    c
0      hello       haha   no
1     heello       hehe   no
2  hello you  haha hehe  yes

in your case:

df["alike"] = df.apply(
    lambda row: "watermelon" if "watermelon" in row["name"] and "melon" in row["alike"] else row["alike"],
    axis=1
)
ted
  • 13,596
  • 9
  • 65
  • 107