使用jupyter notebook,想選擇dataframe某列,比如想選擇列索引為close的資料,使用df['close']或df.close均報錯,小白表示不太看得懂報錯資訊,求大佬們幫著找下原因
from pandas_datareader import data
import datetime
import pandas
from bokeh.plotting import figure, show, output_file
start=datetime.datetime(2016,3,1)
end=datetime.datetime(2016,3,3)
df=data.DataReader(name="AAPL",data_source="yahoo",start=start,end=end)
print(df)
結果為 High Low Open Close Volume Adj Close
Date
2016-02-29 24.557501 24.162500 24.215000 24.172501 140865200 22.516653
2016-03-01 25.192499 24.355000 24.412500 25.132500 201628400 23.410894
2016-03-02 25.222500 24.910000 25.127501 25.187500 132678400 23.462128
2016-03-03 25.427500 25.112499 25.145000 25.375000 147822800 23.636778
df.loc[:,df['close']]
報錯:
KeyError Traceback (most recent call last)
D:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2897 try:
-> 2898 return self._engine.get_loc(casted_key)
2899 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: 'close'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-13-dd7da0aa3318> in <module>
----> 1 df.loc[:,df['close']]
D:\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2904 if self.columns.nlevels > 1:
2905 return self._getitem_multilevel(key)
-> 2906 indexer = self.columns.get_loc(key)
2907 if is_integer(indexer):
2908 indexer = [indexer]
D:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2898 return self._engine.get_loc(casted_key)
2899 except KeyError as err:
-> 2900 raise KeyError(key) from err
2901
2902 if tolerance is not None:
KeyError: 'close'
改成
df.loc[:,df.close]
報錯
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-14-c3455fc984af> in <module>
----> 1 df.loc[:,df.close]
D:\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5139 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5140 return self[name]
-> 5141 return object.__getattribute__(self, name)
5142
5143 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'close'
再改,前面加一行陳述句:
features = list(df.columns)
print(features)
['High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close']
再取列索引:
df.loc[:,features[2:4]]
得出結果:
Open Close
Date
2016-02-29 24.215000 24.172501
2016-03-01 24.412500 25.132500
2016-03-02 25.127501 25.187500
2016-03-03 25.145000 25.375000
uj5u.com熱心網友回復:
知道了,close首字母應該大寫 。。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237771.html
標籤:其他開發語言
上一篇:需要把氨基酸序列隨機翻譯成基因序列,代碼如下但是運行不出來,請問各位大佬問題在哪?
下一篇:matlab保存fig檔案時出錯
