如何解决Python机器学习中列不在索引中的问题

我在Jupyter中运行以下代码:

import pandas as pdimport quandldf=quandl.get('WIKI/GOOGL')print(df.head())#到这里为止代码运行正常,但接下来出现了错误 df=df[['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume',]]df['HL_PCT']=(df['Adj. High']-df['Adj. Low'])/df['Adj. Close']df['PCT_change']=(df['Adj. Close']-df['Adj. Open'])/df['Adj. Open']df=df[['Adj. Close','HL_PCT','PCT_change','Adj.Volume']]print(df.head())

这会生成以下错误:

\local\programs\python\python37-32\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.Try using .loc[row_indexer,col_indexer] = value insteadSee the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy---------------------------------------------------------------------------KeyError  Traceback (most recent call last)<ipython-input-11-c981ac0a05ec> in <module>() 2 df['HL_PCT']=(df['Adj. High']-df['Adj. Low'])/df['Adj. Close']*100.0  3 df['PCT_change']=(df['Adj. Close']-df['Adj. Open'])/df['Adj. Open']*100.0----> 4 df=df[['Adj. Close','HL_PCT','PCT_change','Adj.Volume']]  5 print(df.head())c:\users\xyz\appdata\local\programs\python\python37-32\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)2680         if isinstance(key, (Series, np.ndarray, Index, list)):   2681             # either boolean or fancy integer index-> 2682             return self._getitem_array(key)   2683         elif isinstance(key, DataFrame):  2684             return self._getitem_frame(key)c:\users\xyz\appdata\local\programs\python\python37-32\lib\site-packages\pandas\core\frame.py in _getitem_array(self, key)   2724             return self._take(indexer, axis=0)   2725         else:-> 2726             indexer = self.loc._convert_to_indexer(key, axis=1)   2727             return self._take(indexer, axis=1)   2728 c:\users\xyz\appdata\local\programs\python\python37-32\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter)   1325                 if mask.any():   1326                     raise KeyError('{mask} not in index'-> 1327                                    .format(mask=objarr[mask]))   1328    1329      return com._values_from_object(indexer)KeyError: "['Adj.Volume'] not in index"

你能帮我吗?​


回答:

在’Adj.Volume’中你忘记加空格了,这就是为什么它找不到你指定的列。

这行代码:

df=df[['Adj. Close','HL_PCT','PCT_change','Adj.Volume']]

应该改为:

df=df[['Adj. Close','HL_PCT','PCT_change','Adj. Volume']]

编辑:以下代码可以正常运行:

import pandas as pd import quandl df=quandl.get('WIKI/GOOGL') df=df[['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume']]df['HL_PCT']=(df['Adj. High']-df['Adj. Low'])/df['Adj. Close']df['PCT_change']=(df['Adj. Close']-df['Adj. Open'])/df['Adj. Open']df=df[['Adj. Close','HL_PCT','PCT_change','Adj. Volume']]print(df.head())

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注