我已经检查了类似问题的答案,例如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
我想过滤掉GUID
是nan
的行,然后过滤掉几个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
答案 0 :(得分:1)
您想保留没有拥有的客户:
或
我认为将或运算符更改为和运算符,即:
mask = (customers['GUID'].notnull()) & (customers['address_type']!='POI') & (customers['address_type']!='locality')
当前,在示例的第2行中,地址类型为NaN
,它不等于'POI',因此该行不会被删除。