-1

Let's say I have a pandas dataframe, which looks as follows:

Column1 Column2 Column3
A   1   Y8
B   1   G2
C   1   T1
D   2   A6
E   2   P0
F   3   M2
G   4   O2

I want to remove all values from this dataframe which appear in this list, called 'excl_list', if they are contained in Column3. The idea would be to exclude by the list object and not the individual items in the list:

['A6','P0','M2']

How would I go about doing that?

dataelephant
  • 563
  • 2
  • 7
  • 21
  • 2
    Possible duplicate of [Filter dataframe rows if value in column is in a set list of values](https://stackoverflow.com/questions/12065885/filter-dataframe-rows-if-value-in-column-is-in-a-set-list-of-values) – ALollz Jul 02 '18 at 20:08
  • 2
    `df.loc[~df['Column3'].isin(['A6','P0','M2'])]` ? – harvpan Jul 02 '18 at 20:09
  • Actually, this is probably a more relevant duplicate: https://stackoverflow.com/questions/19960077/how-to-implement-in-and-not-in-for-pandas-dataframe – ALollz Jul 02 '18 at 20:26

1 Answers1

0

As said in the comments, to exclude members of the list from being in Column3:

df.loc[~df['Column3'].isin(excl_list)]

The first part, df['Column3'].isin(excl_list), returns a sequence of True/False terms, one for each row in df. The ~ flips all Trues and Falses, and df.loc[] looks up the trues.

user1717828
  • 7,122
  • 8
  • 34
  • 59