2017-08-28 103 views
1

無上KeyError異常職位的解決方案解決了我的問題,所以這個問題:KeyError異常試圖在大熊貓數據幀訪問新分配的列時

,我有以下的熊貓數據幀列:

df['EventDate'] 

0  26-12-2016 
1  23-12-2016 
2  16-12-2016 
3  15-12-2016 
4  11-12-2016 
5  10-12-2016 
6  07-12-2016 

現在我嘗試使用以下命令將在今年最後四個值分割日期,並提取到另一個系列:

trial=df["EventDate"].str.split("-",2,expand=True) 

現在用第三指數值我能夠讓整個值:

df.year=trial[2] 

現在檢查年列的數據類型:

type(df.year) 
Out[80]: pandas.core.series.Series 

是的,這是熊貓系列通過試轉移[2]代碼的df,。今年

print(trial[2]) 
0  2016 
1  2016 
2  2016 
3  2016 
4  2016 

現在我想GROUPBY Year列那是我得到的錯誤:

yearwise=df.groupby('year') 

Traceback (most recent call last): 

File "<ipython-input-81-cf39b80933c4>", line 1, in <module> 
yearwise=df.groupby('year') 

File "C:\WINPYTH\python-3.5.4.amd64\lib\site- 
packages\pandas\core\generic.py", line 4416, in groupby 
**kwargs) 

File "C:\WINPYTH\python-3.5.4.amd64\lib\site- 
packages\pandas\core\groupby.py", line 1699, in groupby 
return klass(obj, by, **kwds) 

File "C:\WINPYTH\python-3.5.4.amd64\lib\site- 
packages\pandas\core\groupby.py", line 392, in __init__ 
mutated=self.mutated) 

File "C:\WINPYTH\python-3.5.4.amd64\lib\site- 
packages\pandas\core\groupby.py", line 2690, in _get_grouper 
raise KeyError(gpr) 

KeyError: 'year' 

您能否幫忙解決此KeyError並獲取年份列的Groupby值?

萬分感謝您的答案。

+0

你能後的輸出的df.columns和仔細檢查,如果你缺少空間或東西? – Mortz

回答

2

這裏的根本性的誤解是,你認爲這樣做

df.year = ... 

創建一個名爲yeardf列,但這不是真正!觀察:

print(df) 

     Col1 
0 26-12-2016 
1 23-12-2016 
2 16-12-2016 
3 15-12-2016 
4 11-12-2016 
5 10-12-2016 
6 07-12-2016 

df.year = df.Col1.str.split('-', 2, expand=True)[2] 

print(type(df.year)) 
pandas.core.series.Series 

print(df) # where's 'year'?? 

     Col1 
0 26-12-2016 
1 23-12-2016 
2 16-12-2016 
3 15-12-2016 
4 11-12-2016 
5 10-12-2016 
6 07-12-2016 

那麼,什麼是df.year?它是屬性df,它與列不一樣。在python中,你可以使用dot表示法來分配屬性,所以這個工作方式沒有拋出錯誤。您可以通過打印出df.__dict__確認:

print(df.__dict__) 

{ ... 
'year': 0 2016 
1 2016 
2 2016 
3 2016 
4 2016 
5 2016 
6 2016 
Name: 2, dtype: object} 

如果要真正地分配到一列,你需要使用[...]索引語法,就像這樣:

df['year'] = df.Col1.str.split('-', 2, expand=True)[2] 
print(df) 

     Col1 year 
0 26-12-2016 2016 
1 23-12-2016 2016 
2 16-12-2016 2016 
3 15-12-2016 2016 
4 11-12-2016 2016 
5 10-12-2016 2016 
6 07-12-2016 2016 
+0

@jezrael有趣的是,當列存在時,點表示法僅適用於_only_,否則不適用。這可能與大熊貓'__getattr__'和'__hasattr__'實現有關。 –