Remove rows not .isin('X') [duplicate] Remove rows not .isin('X') [duplicate] python python

Remove rows not .isin('X') [duplicate]


You have many options. Collating some of the answers above and the accepted answer from this post you can do:
1. df[-df["column"].isin(["value"])]
2. df[~df["column"].isin(["value"])]
3. df[df["column"].isin(["value"]) == False]
4. df[np.logical_not(df["column"].isin(["value"]))]

Note: for option 4 for you'll need to import numpy as np

Update: You can also use the .query method for this too. This allows for method chaining:
5. df.query("column not in @values").
where values is a list of the values that you don't want to include.


You can use numpy.logical_not to invert the boolean array returned by isin:

In [63]: s = pd.Series(np.arange(10.0))In [64]: x = range(4, 8)In [65]: mask = np.logical_not(s.isin(x))In [66]: s[mask]Out[66]: 0    01    12    23    38    89    9

As given in the comment by Wes McKinney you can also use

s[~s.isin(x)]


All you have to do is create a subset of your dataframe where the isin method evaluates to False:

df = df[df['Column Name'].isin(['Value']) == False]