从 Excel 文件访问数据时出错 - NameError - Name is not defined

时间:2021-05-13 06:26:36

标签: python-3.x

我正在使用 Jupyter Lab 使用 Python 进行简单的线性多元回归。我收到一个错误,它无法识别自变量输入 (SENDR) 之一。回归对自变量 POP65 运行正常,但不是 SENDR。据我所知,这两个变量的标题和列数据在 Excel 中的格式完全相同。感谢您对此的任何启发。

这是我的代码:

import pandas as pd
from statsmodels.formula.api import ols
jma1= pd.read_excel('Regression1.xlsx')
jma1out = ols("USLCSTR ~ POP65 + SENDR", data = jma1).fit(cov_type='HC1')

这是错误代码的一部分:

NameError                                 Traceback (most recent call last)
~\anaconda3\lib\site-packages\patsy\compat.py in call_and_wrap_exc(msg, origin, f, *args, **kwargs)
     35     try:
---> 36         return f(*args, **kwargs)
     37     except Exception as e:

~\anaconda3\lib\site-packages\patsy\eval.py in eval(self, expr, source_name, inner_namespace)
    164         code = compile(expr, source_name, "eval", self.flags, False)
--> 165         return eval(code, {}, VarLookupDict([inner_namespace]
    166                                             + self._namespaces))

<string> in <module>

NameError: name 'SENDR' is not defined

The above exception was the direct cause of the following exception:

PatsyError                                Traceback (most recent call last)
<ipython-input-17-81d657d6069f> in <module>
----> 1 jma1out = ols("USLCSTR ~ POP65 + SENDR", data = jma1).fit(cov_type='HC1')

~\anaconda3\lib\site-packages\statsmodels\base\model.py in from_formula(cls, formula, data, subset, drop_cols, *args, **kwargs)
    167             missing = 'raise'
    168 
--> 169         tmp = handle_formula_data(data, None, formula, depth=eval_env,
    170                                   missing=missing)
    171         ((endog, exog), missing_idx, design_info) = tmp

0 个答案:

没有答案