【问题标题】:How to solve column not in index in python machhine learning如何解决python机器学习中不在索引中的列
【发布时间】:2018-09-28 09:05:53
【问题描述】:

我的 Jupyter 中有以下代码:

import pandas as pd
import quandl
df=quandl.get('WIKI/GOOGL')
print(df.head())

#upto here its working but here comes the error 

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 instead


See 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"

你能帮帮我吗? ​

【问题讨论】:

  • 我向 site 推荐了有关数据科学和机器学习的文章
  • 欢迎来到 SO。请正确格式化您的代码。谢谢!
  • 您确定Adj. Volume 列在您的数据集中吗?

标签: python machine-learning bigdata artificial-intelligence jupyter


【解决方案1】:

在“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())

【讨论】:

  • 即使我尝试使用单个数据框,例如:df=df[['Adj.打开','调整。高','调整。低','调整关闭','调整。 Volume']] 仍然存在同样的错误
猜你喜欢
  • 2018-02-28
  • 2011-03-28
  • 2015-10-23
  • 2020-05-05
  • 2010-10-29
  • 2017-03-03
  • 2019-05-14
  • 2017-06-12
  • 2019-01-06
相关资源
最近更新 更多