KeyError:' DystopiaResidual',大熊猫错误

时间:2017-12-08 03:24:11

标签: python pandas numpy linear-regression

我学习python和线性回归。由于我是初学者,我无法解决问题。

我的代码:

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from sklearn.utils import shuffle
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from sklearn.preprocessing import StandardScaler
from sklearn.manifold import TSNE

from subprocess import check_output

print(check_output(["ls", "input"]).decode("utf8"))
df1 = pd.read_csv("input/2015.csv")
df2 = pd.read_csv("input/2016.csv")
df1.columns
df1.head(2)
sns.regplot(x='Standard Error',y='Happiness Score' ,data=df1)
sns.regplot(x='Economy (GDP per Capita)',y='Happiness Score' ,data=df1)

fr=['Standard Error', 'Economy (GDP per Capita)', 'Family','Health (Life    Expectancy)', 'Freedom', 'Trust (Government Corruption)','Generosity', 'DystopiaResidual']


plt.figure(figsize=(12,28*4))
gs = gridspec.GridSpec(28, 1)
for i, cn in enumerate(fr):
  ax = plt.subplot(gs[i])
  #sns.distplot(df1[cn], bins=50)
  sns.regplot(x=df1[cn],y='Happiness Score' ,data=df1)
  ax.set_xlabel('')
  ax.set_title('Regrassion of feature: ' + str(cn))
plt.show()

错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pandas/core/indexes/base.py",
line 2522, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'DystopiaResidual'

我不明白问题所在。

1 个答案:

答案 0 :(得分:1)

您似乎正在使用世界幸福报告作为样本数据集。

您的代码中有拼写错误。字段引用应为

fr=['Standard Error', 'Economy (GDP per Capita)', 'Family','Health (Life Expectancy)', 'Freedom', 'Trust (Government Corruption)','Generosity', 'Dystopia Residual']

你错过了这个空间。