【问题标题】:Python gives KeyError while the key is passedPython 在传递密钥时给出 KeyError
【发布时间】:2018-03-28 13:47:27
【问题描述】:

我正在尝试创建不同的 python 文件,代码如下所示。在调用该方法时,我将 mydata 作为具有这些列的数据框传递 ['工资','educ','exper','tenure']。

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"

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

【问题讨论】:

  • 删除函数开头的所有这些测试。如果出现问题,会自动引发错误。
  • 缺少最重要的回溯行:第 29 行。
  • 我不明白...我没有从回溯中删除任何行
  • 好吧,它是说工资不在索引中,所以我的想法可能会落到:file[c] 没有钥匙,工资?
  • 我上面已经提到过,工资和参数一样在数据框中传递

标签: python pandas keyerror


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 2019-11-10
    • 1970-01-01
    • 2018-12-27
    • 2021-01-07
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多