Python 尝试';时出现键错误;。pop()';来自数据的列

Python 尝试';时出现键错误;。pop()';来自数据的列,python,pandas,Python,Pandas,这是我的密码: import pandas as pd # path of the training data train_data_path = "C:/Users/User/Desktop/Machine_Learning/Neural_Network/addition_train_data.csv" train_data = pd.read_csv(train_data_path) # loads the data using pandas # sets a target varia

这是我的密码:

import pandas as pd

# path of the training data
train_data_path = "C:/Users/User/Desktop/Machine_Learning/Neural_Network/addition_train_data.csv"
train_data = pd.read_csv(train_data_path)  # loads the data using pandas

# sets a target variable for the ML to predict
# train_target = train_data.pop("Sum")

# path of the evalution data
eval_data_path = "C:/Users/User/Desktop/Machine_Learning/Neural_Network/addition_eval_data.csv"
# loads the data using pandas (again)
eval_data = pd.read_csv(eval_data_path)

# sets a target variable, same as the train_target
eval_target = eval_data.pop("Factor_2")
以下是我的错误:

Traceback (most recent call last):                                                                                                                                                                                                             
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc                                                                                                                                            
return self._engine.get_loc(key)                                                                                                                                                                                                           
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc                                                                                                                                                           
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc                                                                                                                                                           
File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in 
pandas._libs.hashtable.PyObjectHashTable.get_item                                                                                                                              
File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in 
pandas._libs.hashtable.PyObjectHashTable.get_item                                                                                                                            
KeyError: 'Factor_2'                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
During handling of the above exception, another exception occurred:                                                                                                                                                                                                                                                                                                                                                                                                                       
Traceback (most recent call last):                                                                                                                                                                                                             
File "addition.py", line 27, in <module>                                                                                                                                                                                                       
eval_target = eval_data.pop("Factor_2")                                                                                                                                                                                                    
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\generic.py", line 790, in pop                                                                                                                                                      
result = self[item]                                                                                                                                                                                                                        
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__                                                                                                                                               
indexer = self.columns.get_loc(key)                                                                                                                                                                                                        
File "C:\Users\User\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc                                                                                                                                            
return self._engine.get_loc(self._maybe_cast_indexer(key))                                                                                                                                                                                 
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc                                                                                                                                                           
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc                                                                                                                                                           
File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in 
pandas._libs.hashtable.PyObjectHashTable.get_item                                                                                                                              
File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in 
pandas._libs.hashtable.PyObjectHashTable.get_item                                                                                                                            
KeyError: 'Factor_2'                                                                                                                                                                                                   

评价:

Factor_1, Factor_2, Sum
10, 0,
0, 10,
10, 1,
1, 10,
10, 2,
2, 10,
10, 3,
3, 10,
10, 4,
4, 10,
10, 5,
5, 10,
10, 6,
6, 10
10, 7,
7, 10,
10, 8,
8, 10,
10, 9,
9, 10,
10, 10,
20, 20
对于评估数据,“Sum”列为空——由ML来预测


我对Tensorflow和ML还很陌生,非常感谢您的帮助、提示和建议

这是名称的问题。错误信息清楚地表明“因子_2”无效。所以你需要弄清楚实际的列名是什么

使用df.columns获取列名

检查前导空格和尾随空格。逗号是名字的一部分吗

或者,您可以重命名这些列

eval_data.columns = ["Factor_1", "Factor_2", "Sum"]

然后你应该能够弹出你的专栏。

这是一个标准的
pandas
问题,它与
tensorflow
机器学习
无关-请不要垃圾邮件无关的标签(删除)。显然,名称中包含空格。我把它拿走了。现在它起作用了。抓得好;相关的:
eval_data.columns = ["Factor_1", "Factor_2", "Sum"]