2

Trying to replace a string based on column value, getting the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

if df['Target'] == 'U':
   df['Target'] = df['Action'] 

Getting the Error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Just I need to check the string and replace it with another column value if matches

0

2 Answers 2

3

Use np.where

Ex:

import numpy as np

df['Target'] = np.where(df['Target'] == "U", df['Action'], df['Target'])
Sign up to request clarification or add additional context in comments.

Comments

0

You can use pandas.DataFrame.loc:

df.loc[df["Target"] == 'U', "Target"] = df["Action"]

You can also use pandas.DataFrame.where

df["Target"].where(df["Target"] != 'U', df["Action"], inplace = True)

Note that in this case the cells that does NOT satisfy the condition are replaced, that's why you have to use != instead of ==.

Comments

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.