Pandas - ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). What did I miss?

Viewed 462

So I have a pandas dataframe that has

print(data['Campaign Name'])

will return

0 something
1 NaN
2 NaN
3 NaN

so I have a code

data.apply(lambda x : data['Event Code'] if data['Campaign Name'].isna() else data['Campaign Name'])

when I apply this function, they return me an error saying

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

I am not entirely sure how they are ambiguous, as data['Campaign Name'].isna() will return the Series, something like

False
True
True
True

What am I missing?

1 Answers

Don't call the dataframe in apply, call x. Also I expect you meant axis=1 for row-wise calculations, not column wise:

data.apply(lambda x : x['Event Code'] if x['Campaign Name'].isna() else x['Campaign Name'], axis=1)

Also why not just use fillna?

data['Campaign Name'].fillna(data['Event Code'])

fillna fills the values that are None with something else... In this case the Event Code column.

Related