2

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.

Osca
  • 1,588
  • 2
  • 20
  • 41

1 Answers1

1

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)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • what if i want to label data with 1,0 instead of True False, i guess in that situation i need to use a function ? – Osca Oct 14 '20 at 02:20
  • 1
    @Osca chain that with `.astype(int)`: `df['exist'] = df['id'].isin(my_array).astype(int)` – Quang Hoang Oct 14 '20 at 02:22