Osca
Osca

Reputation: 1694

ValueError: The truth value of a Series is ambiguous when using .apply(lambda)

I'm getting value error from this code, which I want to label True if the 'id' is found in my_array

df['exist'] = df['id'].apply(lambda x: True if df['id'].isin(my_array) else False)

I understood the value error may be caused by using 'and', 'or' in the code instead of '&', '|'. However, I'm not using any of these.

Upvotes: 2

Views: 45

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

df['id'].isin(my_array) itself is a series, and if df['id'].isin(my_array) will throw that error because Python doesn't know how to evaluate a series as a single True/False.

Just use isin without any apply:

df['exist'] = df['id'].isin(my_array)

Upvotes: 1

Related Questions