如何做多个`不是'过滤有效

时间:2018-06-08 07:00:18

标签: python pandas dataframe filter

这是我的代码

df = df[df['scorecard_version'] != '9.0']
df = df[df['scorecard_version'] != '8.0']
df = df[df['scorecard_version'] != '10.0']
df = df[df['scorecard_version'] != '11.0']
df = df[df['scorecard_version'] != '11.1']

有没有更短的选择?

1 个答案:

答案 0 :(得分:1)

isin使用反向布尔值掩码~

df[~df['scorecard_version'].isin(['9.0','8.0','10.0','11.0','11.1'])]

使用numpy.in1d的替代解决方案:

df[~np.in1d(df['scorecard_version'].values, ['9.0','8.0','10.0','11.0','11.1'])]