'Value replacement based on multiple conditions
My pandas dataframe looks like this . For each row I want to replace values in Q2 to "positive" if the term "xxpos" occurs within the "SNIPPET" column and if the value in Q2 == 1. Also I want to replace values in Q2 to "negative" if the term "xxneg" occurs within the "SNIPPET" column and the value in Q2 == 1 etc.
I tried a few things, including the following but without success:
df['Q2'] = np.where(("xxpos" in df["SNIPPET"]) & (df['Q2'] == 1) ,"Positive", df['Q2'])
What would be the easiest solution to deal with the multiple conditions?
Solution 1:[1]
Use np.select. This should be the most performant.
conds = [(df['SNIPPET'].str.contains('xxpos')) & (df['Q2'].eq(1)), (df['SNIPPET'].str.contains('xxneg')) & (df['Q2'].eq(1))]
choices = ['Positive', 'Negative']
output = np.select(conds, choices, default=df['Q2'])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mayank Porwal |
