Deleting DataFrame row in Pandas where column value in list

前端 未结 1 1270
失恋的感觉
失恋的感觉 2021-01-22 00:01

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  586         


        
相关标签:
1条回答
  • 2021-01-22 00:26

    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
    
    0 讨论(0)
提交回复
热议问题