我在Mac OSX Lion和Pandas 0.11.0上使用Python 2.7和IPython shell。Python - Pandas'.isin'的名單
我有一個簡短的問題,使用數據選擇方法.isin
。
的問題是,我想的項目列表上使用.isin
,所以:
data = df[df[header[0]].isin(list)]
我收到以下錯誤,當我這樣做:KeyError: u'no item named '
我產生由最初的名單調用以前開發的函數。我嘗試在列表上使用eval
,這似乎解決了在使用raw_input
並迭代其中的項目時出現的問題 - 有點試圖解決我在轉換到IPython
和Python 2.7
(最初使用Python 3.3
)。
我也試過遍歷列表中,首先這樣做的:
data = df[df[header[0]].isin(list[0])]
但也返回:KeyError: u'no item named '
UPDATE: 這裏是頭:
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)
而且,我有一個函數用於獲取標題,這使我更容易,輸出如下所示:
['',
'zipcode',
'xcoord',
'ycoord',
'age_age5064',
'age_age6574',
'age_age75plus',
'sex_female',
'sex_male',
'stage_early',
'stage_late',
'death_death',
'death_not_death',
'access']
實際上,現在我想到了,可能是導致問題的原因 - 儘管eval
仍然無法解決問題。
更新2:
所以,一開始,你可以在上面看到.isin
,我用header[0]
,這是不正確的。我再次嘗試使用header[1]
,這是適當的。我得到以下錯誤:
TypeError: 'int' object is not iterable
我也試圖再次常規列表,並得到這個錯誤:
TypeError: int() argument must be a string or a number, not 'list'
其中,我想,講更明確的問題....
是您'header'一個數據幀?什麼是你'df'和'頭之間的區別er'? – waitingkuo
在'df [df [header [0]]。isin(list)]'內,頭不是'DataFrame'。它是組成標題的列名稱列表。 – DMML
你是如何獲得標題的?通過'df.columns'? – waitingkuo