1

Here's my code

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']

Is there any shorter alternative ?

Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70

1 Answers1

1

Use isin with inverted boolean mask by ~:

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

Alternative solution with numpy.in1d:

df[~np.in1d(df['scorecard_version'].values, ['9.0','8.0','10.0','11.0','11.1'])]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Note `np.in1d` is used by `pd.Series.isin` for sufficiently large arrays ([source](http://github.com/pandas-dev/pandas/blob/v0.23.1/pandas/core/series.py#L3532-L3590)). So there may be benefits to leaving the decision-making to `pd.Series.isin`. – jpp Jul 11 '18 at 11:05