在python中加载数据时出现索引问题
我想计算总回报(上次价格-首次价格)/首次价格。我想直接从yahoo加载数据,而不加载csv文件,然后将其上传到pyhton 你知道为什么会出现这种错误以及如何解决它吗 谢谢大家!在python中加载数据时出现索引问题,python,pandas,dataframe,Python,Pandas,Dataframe,我想计算总回报(上次价格-首次价格)/首次价格。我想直接从yahoo加载数据,而不加载csv文件,然后将其上传到pyhton 你知道为什么会出现这种错误以及如何解决它吗 谢谢大家! import pandas as pd import numpy as np import yfinance as yf import matplotlib.pyplot as plt import datetime as dt start=dt.datetime(2019,5,1) end=dt.dat
import pandas as pd
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
import datetime as dt
start=dt.datetime(2019,5,1)
end=dt.datetime.now()
data= yf.download('TSLA',start,end)['Adj Close']
data = pd.DataFrame(data).dropna()
total_return = (data[-1] - data[0]) / data[0]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\anaconda4\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2894 try:
-> 2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 0
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-45-bb800ce55aa9> in <module>
----> 1 total_return = (data[0]- data[0])/ data[0]
~\anaconda4\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2900 if self.columns.nlevels > 1:
2901 return self._getitem_multilevel(key)
-> 2902 indexer = self.columns.get_loc(key)
2903 if is_integer(indexer):
2904 indexer = [indexer]
~\anaconda4\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
-> 2897 raise KeyError(key) from err
2898
2899 if tolerance is not None:
KeyError: 0
将熊猫作为pd导入
将numpy作为np导入
以yf形式导入yf财务
将matplotlib.pyplot作为plt导入
将日期时间导入为dt
开始=日期时间(2019,5,1)
end=dt.datetime.now()
data=yf.download('TSLA',start,end)['Adj Close']
data=pd.DataFrame(data).dropna()
总返回=(数据[-1]-data[0])/data[0]
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
get\u loc中的~\anaconda4\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2894尝试:
->2895自动返回引擎。获取锁定(铸造键)
2896除KeyError作为错误外:
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
熊猫\\u libs\index.pyx在熊猫中。\ u libs.index.IndexEngine.get_loc()
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
pandas\\u libs\hashtable\u class\u helper.pxi在pandas.\u libs.hashtable.PyObjectHashTable.get\u item()中
关键错误:0
上述异常是以下异常的直接原因:
KeyError回溯(最近一次呼叫最后一次)
在里面
---->1总返回=(数据[0]-数据[0])/数据[0]
~\anaconda4\lib\site packages\pandas\core\frame.py in\uuuuu getitem\uuuuuuuu(self,key)
2900如果self.columns.nlevels>1:
2901返回自我。\u获取项目\u多级(键)
->2902索引器=self.columns.get_loc(键)
2903如果是_整数(索引器):
2904索引器=[索引器]
get\u loc中的~\anaconda4\lib\site packages\pandas\core\index\base.py(self、key、method、tolerance)
2895自动返回引擎。获取锁定(铸造键)
2896除KeyError作为错误外:
->2897从err升起钥匙错误(钥匙)
2898
2899如果公差不是无:
关键错误:0
问题在于您忽略了指定要使用的DF列。
在最后一行中,将对df
的每个引用替换为df[“Adj Close”]
对于代码
data = pd.DataFrame(data).dropna()
print(data["Adj Close"][-1], data["Adj Close"][0])
total_return = ( data["Adj Close"][-1] - data["Adj Close"][0] ) /
data["Adj Close"][0]
print(total_return)
输出是
570.22998046875 46.801998138427734
11.1838810980284
570.22998046875 46.801998138427734
11.1838810980284