我有以下数据框:
load = pd.DataFrame({'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,0],
'D':[1,3,5,4,2,0],
'E':[5,3,6,9,2,4],
'F':list('aaabbb')})
如果条件满足,我需要检查b>c
和c=d
,其中c,d!=0
和False
False
False
True
True
False
都必须为True,否则为False。
输出
var parser, xmlDoc;
var text = "<InventoryResponse><Inventories><a:Inventory><a:ID>12345</a:ID><a:Cost>20.00</a:Cost></a:Inventory></Inventories></InventoryResponse>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
alert(xmlDoc.getElementsByTagName("InventoryResponse")[0].childNodes[1].childNodes[0].childNodes[1].innerHTML);
答案 0 :(得分:1)
我认为需要compilation-speed
为&
链接布尔掩码:
bitwise AND
如果要m = load.B.gt(load.C) & load.C.eq(load.D) & load[['C','D']].ne(0).any(axis=1)
#alternative
m = load.B.gt(load.C) & load.C.eq(load.D) & load['C'].ne(0) & load['D'].ne(0)
print (m)
0 False
1 False
2 False
3 True
4 True
5 False
dtype: bool
获得相同的输出:
numpy.where
如果需要按如下条件设置2个值,则使用它:
load['new'] = np.where(m, True, False)
print (load)
A B C D E F new
0 a 4 7 1 5 a False
1 b 5 8 3 3 a False
2 c 4 9 5 6 a False
3 d 5 4 4 9 b True
4 e 5 2 2 2 b True
5 f 4 0 0 4 b False
性能:
load['new'] = np.where(m, 10, 20)
print (load)
A B C D E F new
0 a 4 7 1 5 a 20
1 b 5 8 3 3 a 20
2 c 4 9 5 6 a 20
3 d 5 4 4 9 b 10
4 e 5 2 2 2 b 10
5 f 4 0 0 4 b 20
答案 1 :(得分:0)
尝试:
load['print'] = load.apply(lambda x:(x.B>x.C)&(x.C==x.D)&(x.C!=0)&(x.D!=0),axis=1)
A B C D E F print
0 a 4 7 1 5 a False
1 b 5 8 3 3 a False
2 c 4 9 5 6 a False
3 d 5 4 4 9 b True
4 e 5 2 2 2 b True
5 f 4 0 0 4 b False
或
load['print'] = np.where((load.B>load.C)&(load.C==load.D)&(load.C!=0)&(load.D!=0),True, False)
答案 2 :(得分:0)
您可以将np.logical_and.reduce
与布尔系列元组一起使用:
m1 = load['B'] > load['C']
m2 = load['C'] == load['D']
m3 = load['C'] != 0
m4 = load['D'] != 0
res = load[np.logical_and.reduce((m1, m2, m3, m4))]
print(res)
A B C D E F
3 d 5 4 4 9 b
4 e 5 2 2 2 b