2017-06-15 112 views
1

當我嘗試執行下面的代碼時,我得到一個KeyError: ('user rating score', 'occurred at index title')回溯。我嘗試在apply()函數中的remove_na_scores之後更改軸,但是沒有任何工作。使用數據幀的KeyError異常

import pandas as pd 
import pprint 

shows = pd.read_csv('/Users/WilliamStevens/Downloads/netflix_shows.csv') 
pprint.pprint(shows.head()) 
shows.info() 

shows_df = shows.groupby(['ratingDescription']).mean() 
print(shows_df) 

missing_user_scores = shows[shows['user rating score'].isnull()] 
mean_scores = shows.groupby(['ratingDescription'])['user rating score'].mean() 

def remove_na_scores(row): 
    if pd.isnull(row['user rating score']): 
     return mean_scores[row['rating']] 
    else: 
     return row['user rating score'] 

shows['user rating score'] = shows.apply(remove_na_scores) 

print(shows['user rating score']) 

完整回溯是如下:

Traceback (most recent call last): 
    File "netflix.py", line 28, in <module> 
shows['user rating score'] = shows.apply(remove_na_scores) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 4163, in apply 
return self._apply_standard(f, axis, reduce=reduce) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py", line 4259, in _apply_standard 
results[i] = func(v) 
    File "netflix.py", line 23, in remove_na_scores 
if pd.isnull(row['user rating score']): 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/series.py", line 601, in __getitem__ 
result = self.index.get_value(self, key) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/indexes/base.py", line 2169, in get_value 
tz=getattr(series.dtype, 'tz', None)) 
    File "pandas/index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:3567) 
    File "pandas/index.pyx", line 113, in pandas.index.IndexEngine.get_value (pandas/index.c:3250) 
    File "pandas/index.pyx", line 163, in pandas.index.IndexEngine.get_loc (pandas/index.c:4373) 
KeyError: ('user rating score', 'occurred at index title') 
+3

請顯示整個回溯,這樣我們知道發生錯誤的位置。 –

+0

回溯(最近最後調用): 文件 「netflix.py」,第28行,在 顯示[ '用戶等級分數'] = shows.apply(remove_na_scores) 文件「/Library/Frameworks/Python.framework/版本/ 3.5/lib/python3.5/site-packages/pandas/core/frame.py「,行4163,in apply return self._apply_standard(f,axis,reduce = reduce) 文件」/ Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/frame.py「,第4259行,在_apply_standard中 results [i] = func(v) 文件」netflix.py「,第23行,在remove_na_scores if pd.isnull(row ['user rating score']): – bullybear17

+0

File「/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core /series.py「,第601行,在__getitem__ result = self.index.get_value(self,key) File」/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/ pandas/indexes/base.py「,第2169行,在get_value tz = getattr(series.dtype,'tz',None)) pandas.index.IndexEngine中的文件」pandas/index.pyx「,第105行。 get_value(pandas/index.c:3567) – bullybear17

回答

0

行:

if pd.isnull(row['user rating score']): 

是在錯誤發生。

發生這種情況是因爲remove_na_scores正沿着錯誤的軸應用。添加, axis=1shows.apply應該解決了您發佈跟蹤問題:

>>>df = pd.DataFrame(np.random.rand(4,2), columns = ['title', 'user rating score']) 
>>>df.apply(lambda x: x['user rating score']) 
... 
KeyError: ('user rating score', 'occurred at index title') 

>>>df.apply(lambda x: x['user rating score'], axis=1) 
0 0.083195 
1 0.243666 
2 0.572457 
3 0.885327 
dtype: float64 

既然你提到指定軸不工作,我想你要跟隨另一個錯誤。如果您描述新問題,我可以編輯我的帖子,但這應該解決所描述的問題。