Python 错误:在pandas.\u libs.hashtable.PyObjectHashTable.get\u项中

Python 错误:在pandas.\u libs.hashtable.PyObjectHashTable.get\u项中,python,python-3.x,pandas,Python,Python 3.x,Pandas,我正在使用Python 3.6.3。我第一次尝试运行我的第一个ML代码。这是代码 import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd import sklearn.linear_model #load the data oecd_bli = pd.read_csv("C:/Users/rschuell/Desktop/oecd_bli.csv",th

我正在使用Python 3.6.3。我第一次尝试运行我的第一个ML代码。这是代码

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

#load the data
oecd_bli = pd.read_csv("C:/Users/rschuell/Desktop/oecd_bli.csv",thousands=',')
gdp_per_capita = pd.read_csv("C:/Users/rschuell/Desktop/gdp_per_capita.csv",thousands=',',delimiter='\t',encoding='latin1',na_values='n/a')

#prepare the data
def prepare_country_stats(oecd_bli, gdp_per_capita):
    #get the pandas dataframe of GDP per capita and Life satisfaction
    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)
    return full_country_stats[["GDP per capita", 'Life satisfaction']]
    
country_stats = prepare_country_stats(oecd_bli, gdp_per_capita) 
#regularization remove_indices = [0, 1, 6, 8, 33, 34, 35]
country_stats.to_csv('country_stats.csv',encoding='utf-8')
X = np.c_[country_stats["GDP per capita"]]
Y = np.c_[country_stats["Life satisfaction"]]

#Visualize the data
country_stats.plot(kind='scatter',x='GDP per capita',y='Life satisfaction')

#Select a linear model
lin_reg_model = sklearn.linear_model.LinearRegression()

#Train the model
lin_reg_model.fit(X, Y)

#plot Regression model
t0, t1 = lin_reg_model.intercept_[0], lin_reg_model.coef_[0][0]
X = np.linspace(0, 110000, 1000)
plt.plot(X, t0 + t1 * X, "k")
plt.show()

#Make a prediction for Cyprus
X_new=[[22587]]
print(lin_reg_model.predict(X_new))


import sys
print (sys.version)
我是从下面的链接得到的

当我运行代码时,我在这里得到一个错误:

country_stats = prepare_country_stats(oecd_bli, gdp_per_capita) 
#regularization remove_indices = [0, 1, 6, 8, 33, 34, 35]
country_stats.to_csv('country_stats.csv',encoding='utf-8')
X = np.c_[country_stats["GDP per capita"]]
Y = np.c_[country_stats["Life satisfaction"]]
错误消息如下:

 File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'Country'
我认为变量是读入的,并且分配正确


我不知道是什么问题。我如何才能让它运行?

您的程序对我来说非常适合,并显示:

[6.28653637]

在以下版本上使用Jypyther: 笔记本服务器的版本为:5.2.2 3.6.3 |由conda forge包装|(默认,2017年11月4日,10:10:56) [GCC 4.8.2 20140120(Red Hat 4.8.2-15)]

只想我改变了第八行:

oecd_bli = pd.read_csv("oecd_bli.csv",thousands=',')
致:


记住从正确下载数据集,因为当我第一次下载时,并没有csv格式的文件。请在查看器中再次检查它们是否确实正确。(我选择原始文件并直接从浏览器下载。)

您的程序非常适合我,并显示:

[6.28653637]

在以下版本上使用Jypyther: 笔记本服务器的版本为:5.2.2 3.6.3 |由conda forge包装|(默认,2017年11月4日,10:10:56) [GCC 4.8.2 20140120(Red Hat 4.8.2-15)]

只想我改变了第八行:

oecd_bli = pd.read_csv("oecd_bli.csv",thousands=',')
致:


记住从正确下载数据集,因为当我第一次下载时,并没有csv格式的文件。请在查看器中再次检查它们是否确实正确。(我选择原始文件并直接从浏览器下载。)

您确定函数中有2015年的可变人均gdb_列吗?Bo,年份从1960年到2011年。我从这里下载了两个文件:您确定函数中有2015年的可变人均gdb_列吗?Bo,年份从1960年到2011年。我从这里下载了两个文件:是的,是的,我认为这与我的浏览器处理CSV文件的方式有关。我想有些东西被弄坏了。我仍然无法让它工作,但我认为这就是问题所在。那张图表正是我期望看到的!谢谢你的帮助!!是的,是的,我认为这与我的浏览器处理CSV文件的方式有关。我想有些东西被弄坏了。我仍然无法让它工作,但我认为这就是问题所在。那张图表正是我期望看到的!谢谢你的帮助!!