Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop rows by list in pandas [duplicate]

Now I have dataframe and list.

A B
1 a 
2 b
3 c
4 d
5 e

list=[a,b,c]

I would like to drop rows by df.B refering to list.

I would like to below df

A B
4 d
5 e

How can I get this result?

like image 555
Heisenberg Avatar asked Jan 30 '17 11:01

Heisenberg


People also ask

How can I delete duplicate rows from one column in Pandas?

To remove duplicates of only one or a subset of columns, specify subset as the individual column or list of columns that should be unique. To do this conditional on a different column's value, you can sort_values(colname) and specify keep equals either first or last .


1 Answers

You can use isin with inverted mask by ~.

I think list is not good name in python, better is L, because list is code word and if assign variable you override it:

L= ['a','b','c']

print (df[~df.B.isin(L)])
   A  B
3  4  d
4  5  e
like image 120
jezrael Avatar answered Oct 07 '22 06:10

jezrael