首页 > 解决方案 > KeyError:标准 gdp 学习代码中的 `INEQUALITY`

问题描述

我正在尝试运行 Geron 书的第一个示例,如下所示:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.linear_model

oecd_bli = pd.read_csv("/Users/matin/Desktop/oecd_bli_2015.csv", thousands = ',', error_bad_lines=False)

gdp_per_capita = pd.read_csv("/Users/matin/Desktop/gdp_per_capita.csv", thousands = ',', delimiter='\t',
                             encoding='latin1', na_values="n/a")

def prepare_country_stats(oecd_bli, gdp_per_capita):
    oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
    oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")
    gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
    gdp_per_capita.set_index("Country", inplace=True)
    full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita,
                                  left_index=True, right_index=True)
    full_country_stats.sort_values(by="GDP per capita", inplace=True)
    remove_indices = [0, 1, 6, 8, 33, 34, 35]
    keep_indices = list(set(range(36)) - set(remove_indices))
    return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]

country_stats = prepare_country_stats(oecd_bli,gdp_per_capita)
X = np.c_[country_stats["GDP per capita"]]
y = np.c_[country_stats["Life satisfaction"]]

country_stats.plot(kind='scatter', x="GCP per capita",
                   y='Life satisfaction')
plt.show()

model = sklearn.linear_model.LinearRegression()

model.fit(X,y)

我已经下载了整体结构如下的数据集。

在此处输入图像描述

在此处输入图像描述

然而python返回以下错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/matin/PycharmProjects/test/test.py", line 23, in <module>
    country_stats = prepare_country_stats(oecd_bli,gdp_per_capita)
  File "/Users/matin/PycharmProjects/test/test.py", line 12, in prepare_country_stats
    oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'INEQUALITY'

你能指出我做错了什么吗?

以下是文件:

oecd_bli_2015gdp_per_capita

标签: pythonpandaskeyerror

解决方案


推荐阅读