熊猫过滤(~isin)不起作用

时间:2018-02-14 06:57:48

标签: python pandas filter

这是我的数据

 omzet_activity_paid_count_january_devices

No    Text    
1     dcbjkd
2     hdgfhds
3     dsdfs
33    jhsda
40    ydgyua

我希望过滤这样的数据

omzet_activity_paid_count_january_devices

No    Text    
3     dsdfs
40    ydgyua

这就是我做的事情

user_internal = ['1','2','33']
omzet_activity_paid_count_january_devices = omzet_activity_paid_count_january_devices[~omzet_activity_paid_count_january_devices.No.isin(user_internal)]

上面的代码不起作用,输出仍然像我的原始数据帧,我错过了什么?

1 个答案:

答案 0 :(得分:1)

我认为你有混合类型的问题 - 带字符串的数字。

omzet_activity_paid_count_january_devices.No.dtype
dtype('O')

omzet_activity_paid_count_january_devices.No.head().tolist()
[1, 2, 3, 33, 40] 

所以将列No转换为字符串:

user_internal = ['1','2','33']
mask = ~omzet_activity_paid_count_january_devices.No.astype(str).isin(user_internal)
omzet_activity_paid_count_january_devices =  omzet_activity_paid_count_january_devices[mask]

或者将string从列表更改为int s:

user_internal = [1,2,33]
mask = ~omzet_activity_paid_count_january_devices.No.isin(user_internal)
omzet_activity_paid_count_january_devices =  omzet_activity_paid_count_january_devices[mask]