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().