放置行大 pandas 与参数列表 [英] Drop row pandas with a list of argument
本文介绍了放置行大 pandas 与参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将所有不在列表中的行放在大熊猫DataFrame中
I would like to drop all the row which are not in a list in pandas DataFrame
例如,考虑这个数据框:
For instance, consider this dataframe :
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'year': [2012, 2012, 2013, 2014, 2014],
'reports': [4, 24, 31, 2, 3]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df
按名称删除一行很简单:
To drop a row by name it's easy :
df = df[df.name != 'Tina'] # to drop the row which include Tina in the name column
但如果我只想保留行Jason和Molly:
But if I want to keep only the row Jason and Molly :
List=['Jason', 'Molly']
df = df[df.name not in List]
不起作用
推荐答案
使用 isin
,并将该列表作为参数传递,并反转条件使用〜
:
In [58]:
names = ['Jason', 'Molly']
df[~df['name'].isin(names)]
Out[58]:
name reports year
Santa Cruz Tina 31 2013
Maricopa Jake 2 2014
Yuma Amy 3 2014
这篇关于放置行大 pandas 与参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文