密钥传递时,Python会给出KeyError

时间:2018-03-28 13:47:27

标签: python pandas keyerror

我正在尝试创建不同的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"

任何人都可以帮我解释为什么我会收到此错误。我该如何解决?

1 个答案:

答案 0 :(得分:1)

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

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