[英]jupyter notebook keyerror: 148 on running lambda
我一直在嘗試學習舊的 github 帖子,我已經解決了大多數問題:
def create_location_count(features):
for feat in features:
loc_count = data.pivot_table(values=feat, index='location',aggfunc=lambda x: sum(x>0))
data['loc_count_%s'%feat] = data['location'].apply(lambda x: loc_count[x])
def create_location_stats(features):
for feat in features:
loc_mean = data.pivot_table(values=feat, index='location',aggfunc=lambda x: np.mean([i for i in x if i>0]) if sum(x>0)>0 else 0)
data['loc_mean_%s'%feat] = data['location'].apply(lambda x: loc_mean[x])
當我運行它時(在 Jupyter 筆記本中):
new_feat = ['feature 203','feature 82','feature 170','feature 54',
'feature 201','feature 80','feature 71','feature 193','feature 54','feature 312','feature 202',
'feature others_1','feature 232','feature 68', 'feature others_0','feature 307']
create_location_stats(new_feat)
我收到此錯誤: KeyError: 148
我認為您的第一個嫌疑人是data['location'].apply(lambda x: loc_count[x])
您正在使用location
的元素(可能是整數)來索引loc_count
。 同樣的事情發生在下一個 for 循環中。
生成的 KeyError 指出loc_count
不包含密鑰 148。
我希望這些提示對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.