按多种条件过滤行-为什么不起作用?

时间:2019-12-13 12:48:29

标签: python pandas

我已经检查了类似问题的答案,例如here,但仍然无法使用此过滤器。我知道我做错了事,但找不到。

print(customers[['GUID','address_type']].head())

                                   GUID      address_type
0  4c07d11e-2d5c-851d-e053-ca3ca8c0ca7f     StreetAddress
1  4c07d11e-3bac-851d-e053-ca3ca8c0ca7f  StreetAddressExt
2                                   NaN               NaN
3  4c07d11e-2d5c-851d-e053-ca3ca8c0ca7f     StreetAddress
4                                   NaN               NaN

我想过滤掉GUIDnan的行,然后过滤掉几个address_types的行。

mask = (customers['GUID'].notnull()) | (customers['address_type']!='POI') | (customers['address_type']!='locality')

customers = customers[mask].copy()

现在让我们检查结果:

print(customers[['GUID','address_type']].head())

                                   GUID      address_type
0  4c07d11e-2d5c-851d-e053-ca3ca8c0ca7f     StreetAddress
1  4c07d11e-3bac-851d-e053-ca3ca8c0ca7f  StreetAddressExt
2                                   NaN               NaN
3  4c07d11e-2d5c-851d-e053-ca3ca8c0ca7f     StreetAddress
4                                   NaN               NaN

我在nan中还有GUID……这里是怎么回事?如果我仅设置一个customers['GUID'].notnull())的简单过滤器,它将起作用。但是结合条件没有任何作用。有什么想法吗?

到目前为止,这实际上是我的所有代码(我已经添加了额外的jezrels !='NaN),在后台没有发生任何疯狂的事情:

import pandas as pd

customers = pd.read_csv('data\latest_geocoded.csv',low_memory=False)

customers[['GUID','address_type']].head()

mask = (customers['GUID'].notnull()) | (customers['GUID']!='NaN') | (customers['address_type']!='POI') | (customers['address_type']!='locality')

customers = customers[mask].copy()

print(customers[['GUID','address_type']].head())

                                   GUID      address_type
0  4c07d11e-2d5c-851d-e053-ca3ca8c0ca7f     StreetAddress
1  4c07d11e-3bac-851d-e053-ca3ca8c0ca7f  StreetAddressExt
2                                   NaN               NaN
3  4c07d11e-2d5c-851d-e053-ca3ca8c0ca7f     StreetAddress
4                                   NaN               NaN

1 个答案:

答案 0 :(得分:1)

您想保留没有拥有的客户:

  1. “ GUID”的空值

  1. {'POI','locality'}中的地址类型

我认为将运算符更改为运算符,即:

mask = (customers['GUID'].notnull()) & (customers['address_type']!='POI') & (customers['address_type']!='locality')

当前,在示例的第2行中,地址类型为NaN,它不等于'POI',因此该行不会被删除。