删除Pandas中的DataFrame行,其中列值在列表中 [英] Deleting DataFrame row in Pandas where column value in list
本文介绍了删除Pandas中的DataFrame行,其中列值在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在列值位于列表中的Pandas数据框中删除某些行?例如:
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
推荐答案
使用 isin
与
mylist = ['fh3', 'fh1', 'fh4']
print (df[~df.id.isin(mylist)])
id loc_id
0 fh0 859
2 fh2 2585
4 fh5 586
使用 drop
的另一种解决方案:
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
这篇关于删除Pandas中的DataFrame行,其中列值在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文