问题描述
A B C D
0 0.397333 Xor 0.569748 0.406415
1 0.319684 x 0.159117 0.522648
2 0.778038 0.486989 x x
3 0.549993 0.896913 0.960814 0.430113
4 0.251655 0.802137 Xand 0.218265
在这里,我需要比较所有四列,并且需要有一个新的column E
,我将在其中获得新的信息。
我需要检查是否四列中的任何一个包含x
而Column E
值Yes
else No
输出量
A B C D E
0 0.397333 Xor 0.569748 0.406415 No
1 0.319684 x 0.159117 0.522648 Yes
2 0.778038 0.486989 x x Yes
3 0.549993 0.896913 0.960814 0.430113 No
4 x 0.802137 Xand 0.218265 Yes
我想在这里使用where子句,但我无法做到这一点,而lambda也无法理解应如何编写。
这是我的代码:
def YorN(stri):
if stri =='x':
return True
else:
return False
df['E'] = np.where(YorN(df.B) | YorN(df.C) | YorN(df.D)| YorN(df.A), 'Yes', 'No')
错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
编辑1我的其他列可能包含其他一些变量
1楼
如您所知,您的比较函子无法正常工作,您正在尝试将标量与数组进行比较。
无论如何,您可以调用apply
并传递axis=1
来逐行处理df。
将str.contains
转换为str
以便可以str.contains
量化的str.contains
与any
一起使用以生成布尔序列,并将其用作np.where
的arg,并分别在True
或False
时返回“ yes”或“ no”:
In [8]:
df['E'] = np.where(df.astype(str).apply(lambda x: x.str.contains('x').any(), axis=1), 'yes', 'no')
df
Out[8]:
A B C D E
0 0.397333 0.245596 0.569748 0.406415 no
1 0.319684 x 0.159117 0.522648 yes
2 0.778038 0.486989 x x yes
3 0.549993 0.896913 0.960814 0.430113 no
4 0.251655 0.802137 0.024341 0.218265 no
编辑
答案仍然有效:
In [10]:
df['E'] = np.where(df.astype(str).apply(lambda x: x.str.contains('x').any(), axis=1), 'yes', 'no')
df
Out[10]:
A B C D E
0 0.397333 Xor 0.569748 0.406415 no
1 0.319684 x 0.159117 0.522648 yes
2 0.778038 0.486989 x x yes
3 0.549993 0.896913 0.960814 0.430113 no
4 0.251655 0.802137 Xand 0.218265 no