python机器学习中如何解决列不在索引中
我的Jupyter中有以下代码:python机器学习中如何解决列不在索引中,python,machine-learning,bigdata,artificial-intelligence,jupyter,Python,Machine Learning,Bigdata,Artificial Intelligence,Jupyter,我的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']-
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"
\local\programs\python\python37-32\lib\site packages\ipykernel\u launcher.py:2:SettingWithCopyWarning:
试图在数据帧切片的副本上设置值。
尝试改用.loc[row\u indexer,col\u indexer]=value
请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-查看与复制
---------------------------------------------------------------------------
关键错误
回溯(最近一次呼叫最后一次)
在()
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打印(df.head())
c:\users\xyz\appdata\local\programs\python\python37-32\lib\site-
packages\pandas\core\frame.py in\uuuuu getitem\uuuuu(self,key)
2680如果isinstance(键,(系列,np.ndarray,索引,列表)):
2681#布尔或奇异整数索引
->2682返回self.\u getitem\u数组(键)
2683 elif isinstance(键,数据帧):
2684返回自我。\u获取项目\u帧(键)
c:\users\xyz\appdata\local\programs\python\python37-32\lib\site packages\pandas\core\frame.py在\u getitem\u数组中(self,key)
2724返回自取(索引器,轴=0)
2725其他:
->2726索引器=self.loc.\u转换为索引器(键,轴=1)
2727
返回自取(索引器,轴=1)
2728
c:\users\xyz\appdata\local\programs\python37-32\lib\site packages\pandas\core\index.py in\u convert\u to\u indexer(self、obj、axis、is\u setter)
1325如果掩码为。任何()
1326 raise KeyError(“{mask}不在索引中”
->1327.格式(mask=objarr[mask]))
1328
1329从对象(索引器)返回com.\U值
KeyError:“['Adj.Volume']不在索引中”
你能帮我吗?
在“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())
我推荐这篇关于数据科学和机器学习的文章。请正确格式化您的代码。谢谢您确定列
Adj。卷
在您的数据集中?即使我尝试使用单个数据帧,如:df=df[['Adj.Open'、'Adj.High'、'Adj.Low'、'Adj.Close'、'Adj.Volume']]仍然存在相同的错误