Python-清单上的Pandas'.isin' [英] Python - Pandas '.isin' on a list

查看:100
本文介绍了Python-清单上的Pandas'.isin'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有IPython shell的Mac OSX Lion和Pandas 0.11.0上使用Python 2.7.

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

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

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

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

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

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

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

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

我通过调用以前开发的函数来生成初始列表.我尝试使用列表中的eval,这似乎解决了使用raw_input并遍历其中的项目时出现的问题-试图解决我在过渡到Python 2.7(原为Python 3.3).

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

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

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

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

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

更新: 这是标题:

 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']

实际上,我现在考虑的可能是导致问题的原因-尽管eval仍无法解决.

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

更新2:

因此,起初,如您在上面的.isin中所见,我使用的是header[0],这是不对的.我再次尝试使用header[1],这是合适的.我收到以下错误:

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:

 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....

推荐答案

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

Try to use df.columns as your header instead:

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

这篇关于Python-清单上的Pandas'.isin'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆