Python - Pandas'.isin'列表中

DMM*_*MML 3 python-2.7 pandas

我在Mac OSX Lion上使用Python 2.7,在IPython shell上使用Pandas 0.11.0.

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

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

data = df[df[header[0]].isin(list)]
Run Code Online (Sandbox Code Playgroud)

我这样做时收到以下错误: KeyError: u'no item named '

我通过调用以前开发的函数生成初始列表.我尝试使用eval列表中,这似乎解决涉及使用时的一个问题上raw_input并遍历其内部的物品-有点儿试图找出一些过渡到时候我一直有问题IPython,并Python 2.7(最初使用Python 3.3).

我也尝试迭代列表,首先做:

data = df[df[header[0]].isin(list[0])]
Run Code Online (Sandbox Code Playgroud)

但这也会回归: 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)
Run Code Online (Sandbox Code Playgroud)

此外,我有一个函数,我用来获取标题,这使我更容易,输出如下所示:

['',
  'zipcode',
  'xcoord',
  'ycoord',
 'age_age5064',
 'age_age6574',
 'age_age75plus',
 'sex_female',
 'sex_male',
 'stage_early',
 'stage_late',
 'death_death',
 'death_not_death',
 'access']
Run Code Online (Sandbox Code Playgroud)

实际上,现在,考虑到它,可能是造成问题的原因 - 尽管eval仍然无法解决问题.

更新2:

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

 TypeError: 'int' object is not iterable
Run Code Online (Sandbox Code Playgroud)

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

TypeError: int() argument must be a string or a number, not 'list'
Run Code Online (Sandbox Code Playgroud)

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

wai*_*kuo 7

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

df[df[df.columns[1]].isin(list)]
Run Code Online (Sandbox Code Playgroud)