It seems like I keep coming across this same problem. I have a dataframe in which I have two particular columns. I want to create a new column which uses either a value from one of the two columns or the other based on a boolean expression. A simple example:
import pandas as pd
df = pd.DataFrame({'A': ['Fear', 'Surprise', 'Fear', 'Fear'],
'B': ['Efficiency', 'Efficiency', 'Efficiency', 'Devotion'],
'C': [True, True, False, False]})
df['D'] = df['A'] if df['C'] else df['B']
That last line is pseudocode for what I want to do. If I run this code, it complains about the truth of a series being ambiguous in that last line. Desired output in column 'D' is ['Fear', 'Surprise', 'Efficiency', 'Devotion']
I have a feeling there is a simple solution to this problem, but I have yet to come across it.