df。 drop 导致 & 的操作数类型不受支持:'float' 和 'bool'

df. drop results in unsupported operand type(s) for &: 'float' and 'bool'

我想删除具有以下条件的行,但遇到以下错误: 我们如何删除具有 2 个不同类型的条件的行?

result['LastDigit']= result['IP'].str.strip().str[-1].astype(int)

result = result.drop(result[result['Type']=='A'] & result[result['LastDigit'] %2 ==0], axis=1)

错误:

Exception has occurred: TypeError
unsupported operand type(s) for &: 'float' and 'bool'

样本:

    UniqueCode  ID     IP_Address       Name    Type LastDigit
0      QQ       22     172.16.1.67      Name1     A     7
2      XX       33     172.2.12.68      Name2     A     8
4      ZZ       44     10.21.22.2       Name3     B     2

使用 boolean indexing 并通过 ~ 反转原始掩码,并添加 () 因为运算符的优先级:

result = result[~((result['Type']=='A') & (result['LastDigit'] %2 ==0))]

或反转测试掩码不等于 | 按位 OR:

result = result[(result['Type']!='A') | (result['LastDigit'] %2 !=0)]
print (result)
  UniqueCode  ID   IP_Address   Name Type  LastDigit
0         QQ  22  172.16.1.67  Name1    A          7
4         ZZ  44   10.21.22.2  Name3    B          2