新的熊猫专栏 - Numpy.where有多个条件

时间:2017-12-17 17:44:23

标签: python pandas numpy

我有一个数据帧,我想使用np.where(numpy)来执行逻辑测试。我的数据框是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   

我想使用具有以下条件的np.where创建一个新列:

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")

但是我收到以下错误:

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

源于test_df['A'] == 0 & test_df['B'] == 0。我不知道如何解决这个问题,我想使用np.where。我很感激帮助!

0 个答案:

没有答案