Skip to main content
 首页 » 编程设计

Python 在传递 key 时给出 KeyError

2025年01月19日10cloudgamer

我正在尝试创建下面给出代码的不同 python 文件。在调用该方法时,我将 mydata 作为数据框与这些列一起传递 ['工资','教育','专家','任期']。

import pandas as pd 
import numpy as np 
from prettytable import PrettyTable as pt 
 
def LinearRegressionOLS(mydata,target_column): 
 
    if(not isinstance(mydata,pd.DataFrame)): 
        raise TypeError("Data must be of type Data Frame") 
 
    if(not isinstance(target_column,str)): 
        raise TypeError("target_column must be String") 
 
    if(target_column not in mydata.columns): 
        raise KeyError("target_column doesn't exist in Data Frame") 
 
    data=mydata.copy() 
 
    data["one"]=np.ones(data.count()[target_column]) 
 
    column_list=["one"] 
    for i in data.columns: 
        column_list.append(i) 
 
    Y=data[target_column].as_matrix() 
    data.drop(target_column,inplace=True,axis=1) 
    X=data[column_list].as_matrix() 
    del data 
 
    beta = np.matmul(np.matmul(np.linalg.inv(np.matmul(X.T,X)),X.T),Y) 
    predY = np.matmul(X,beta) 
    total = np.matmul((Y-np.mean(Y)).T,(Y-np.mean(Y))) 
    residual = np.matmul((Y-predY).T,(Y-predY)) 
    sigma = np.matmul((Y-predY).T,(Y-predY))/(X.shape[0]-X.shape[1]) 
    omega = np.square(sigma)*np.linalg.inv(np.matmul(X.T,X)) 
    SE = np.sqrt(np.diag(omega)) 
    tstat = beta/SE 
    Rsq = 1-(residual/total) 
 
    final = pt() 
    final.add_column(" ",column_list) 
    final.add_column("Coefficients",beta) 
    final.add_column("Standard Error",SE) 
    final.add_column("t-stat",tstat) 
 
    print(final) 
    print("Residual: ",residual) 
    print("Total: ",total) 
    print("Standard Error: ",sigma) 
    print("R Square: ",Rsq) 

运行上面的代码后,通过调用下面给出的函数,

    >>> c 
    ['wage', 'educ', 'exper', 'tenure'] 
    >>> import LR_OLS as inf 
    >>> inf.LinearRegressionOLS(file[c],"wage") 

,我遇到这样的错误

    Traceback (most recent call last): 
    File "<pyshell#182>", line 1, in <module> 
        inf.LinearRegressionOLS(file[c],"wage") 
    File "E:\python\LR_OLS.py", line 29, in LinearRegressionOLS 
    File "C:\Program Files\Python35\lib\site-packages\pandas\core\frame.py", line 2133, in __getitem__ 
        return self._getitem_array(key) 
    File "C:\Program Files\Python35\lib\site-packages\pandas\core\frame.py", line 2177, in _getitem_array 
        indexer = self.loc._convert_to_indexer(key, axis=1) 
    File "C:\Program Files\Python35\lib\site-packages\pandas\core\indexing.py", line 1269, in _convert_to_indexer 
        .format(mask=objarr[mask])) 
    KeyError: "['wage'] not in index" 

谁能帮我解释为什么会出现这个错误。我该如何解决?

请您参考如下方法:

问题是您在 'column_list 中仍然有 'wage'。因此,为了永远不会让它进入那里,请进行以下调整:

for i in data.columns: 
    if i != 'wage':  # add this line to your code 
       column_list.append(i)