2

How to remove certain rows in Pandas dataframe where column value is in list? For example:

given mylist = [fh3, fh1, fh4]

   id  loc_id
0  fh0  0859
1  fh1  5861
2  fh2  2585
3  fh3  853
4  fh4  45596
4  fh5  586

remove rows where id in mylist:

   id  loc_id
0  fh0  0859
2  fh2  2585
4  fh5  586

1 Answer 1

4

Use isin with boolean indexing:

mylist = ['fh3', 'fh1', 'fh4']
print (df[~df.id.isin(mylist)])
    id  loc_id
0  fh0     859
2  fh2    2585
4  fh5     586

Another solution with drop:

mylist = ['fh3', 'fh1', 'fh4']
print (df.set_index('id').drop(mylist).reset_index())
    id  loc_id
0  fh0     859
1  fh2    2585
2  fh5     586
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.