0

I have a dataframe and I want to to use np.where (numpy) to perform a logical test. My dataframe is test_df:

    A   B   thing
0   0   0   
1   1   1   
2   2   4   
3   3   9   
4   4   16  
5   5   25  ma
6   6   36  
7   7   49  
8   8   64  
9   9   81   

I want to create a new column using the np.where with the following conditions:

test_list = ['Me', 'ma']
test_df['new_col'] = np.where((test_df['A'] == 0 & test_df['B'] == 0 & test_df['thing'].isin(test_list)) | test_df['B'] == 1, "Yep", "Nope")

But I get the following error:

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

Which stems from test_df['A'] == 0 & test_df['B'] == 0. I'm not sure how to resolve this and I'd like to use np.where. I appreciate the help!

Ryan Erwin
  • 807
  • 1
  • 11
  • 30

0 Answers0