Python - 列表中的 Pandas '.isin'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16804476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:09:52  来源:igfitidea点击:

Python - Pandas '.isin' on a list

python-2.7pandas

提问by DMML

I'm using Python 2.7 on Mac OSX Lion and Pandas 0.11.0 with the IPython shell.

我在 Mac OSX Lion 和 Pandas 0.11.0 上使用 Python 2.7 和 IPython shell。

I have a brief issue, using the data selection method .isin.

我有一个简短的问题,使用数据选择方法.isin

The issue is that I would like to use .isinon a list of items, so:

问题是我想.isin在项目列表上使用,所以:

data = df[df[header[0]].isin(list)]

I get the following error when I do this: KeyError: u'no item named '

执行此操作时出现以下错误: KeyError: u'no item named '

I generate the initial list by calling a previously developed function. I tried using evalon the list, which seems to solve an issue that comes about when using raw_inputand iterating over items within it - kinda trying to work out some of the issues I've been having when transitioning to IPythonand Python 2.7(originally used Python 3.3).

我通过调用以前开发的函数来生成初始列表。我尝试eval在列表上使用,这似乎解决了在使用raw_input和迭代其中的项目时出现的问题- 有点试图解决我在过渡到IPythonPython 2.7(最初使用Python 3.3)时遇到的一些问题。

I also tried iterating over the list, by first doing:

我还尝试遍历列表,首先执行以下操作:

data = df[df[header[0]].isin(list[0])]

But that also returns: KeyError: u'no item named '

但这也会返回: KeyError: u'no item named '

UPDATE: Here is the header:

更新:这是标题:

 Unnamed: 0         9752  non-null values
 zipcode            9752  non-null values
 xcoord             9752  non-null values
 ycoord             9752  non-null values
 age_age5064        9752  non-null values
 age_age6574        9752  non-null values
 age_age75plus      9752  non-null values
 sex_female         9752  non-null values
 sex_male           9752  non-null values
 stage_early        9752  non-null values
 stage_late         9752  non-null values
 death_death        9752  non-null values
 death_not_death    9752  non-null values
 access             9752  non-null values
 dtypes: float64(2), int64(12)

Also, I have a function I use to get the header, which makes things easier for me, the output looks like this:

另外,我有一个用于获取标题的函数,这对我来说更容易,输出如下所示:

['',
  'zipcode',
  'xcoord',
  'ycoord',
 'age_age5064',
 'age_age6574',
 'age_age75plus',
 'sex_female',
 'sex_male',
 'stage_early',
 'stage_late',
 'death_death',
 'death_not_death',
 'access']

Which, actually, now that I think about it, may be what is causing the problem- although evalstill would not fix it.

实际上,现在我考虑了一下,这可能是导致问题的原因-尽管eval仍然无法解决它。

UPDATE 2:

更新 2:

So, initially, as you can see in the above .isin, I was using header[0], which was not right. I tried again using header[1], which is appropriate. I get the following error:

所以,最初,正如你在上面看到的.isin,我使用的是header[0],这是不对的。我再次尝试使用header[1],这是合适的。我收到以下错误:

 TypeError: 'int' object is not iterable

I also tried the regular list again and got this error:

我也再次尝试了常规列表并得到了这个错误:

TypeError: int() argument must be a string or a number, not 'list'

Which, I guess, speaks more definitively to the issue....

我想,哪个更明确地说明了这个问题......

回答by waitingkuo

Try to use df.columns as your header instead:

尝试使用 df.columns 作为标题:

df[df[df.columns[1]].isin(list)]