Traceback (most recent call last): File "d:\Users\lenovo\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3629, in get_loc return self._engine.get_loc(casted_key) File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: ('User_id', 'Coupon_id', 'max_date_received', 'min_date_received') The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\python相关2\任务二.py", line 251, in <module> train = get_dataset(train_history_field, train_middle_field, train_label_field) File "D:\python相关2\任务二.py", line 147, in get_dataset user_feat = get_user_feat(label_field) # 用户特征 File "D:\python相关2\任务二.py", line 69, in get_user_feat t2=t2['User_id','Coupon_id','max_date_received','min_date_received'] File "d:\Users\lenovo\anaconda3\lib\site-packages\pandas\core\frame.py", line 3505, in __getitem__ indexer = self.columns.get_loc(key) File "d:\Users\lenovo\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 3631, in get_loc raise KeyError(key) from err KeyError: ('User_id', 'Coupon_id', 'max_date_received', 'min_date_received')
时间: 2023-09-10 10:13:42 浏览: 170
Mac下anaconda中Spyder打开报错Traceback(most recent call last)…问题
5星 · 资源好评率100%
这段代码运行出错了,错误提示是 KeyError,意思是字典或者 DataFrame 中没有这个 key。具体原因是在 get_user_feat 函数中调用了 t2['User_id','Coupon_id','max_date_received','min_date_received'],但是 DataFrame 对象 t2 中没有这个 key。这个错误可能是因为在调用 t2['User_id','Coupon_id','max_date_received','min_date_received'] 时,需要将 key 用中括号括起来,即 t2[['User_id','Coupon_id','max_date_received','min_date_received']]。
阅读全文