我有一個叫做moving_average
的python熊貓系列。訪問這個python熊貓系列的最後一個元素
moving_average = df['score'].rolling(window=period).mean()
我想檢索系列moving_average
的最後一個元素。這就是我所做的。
moving_average = df['score'].rolling(window=period).mean()[-1]
不幸的是,我得到了以下錯誤。
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py", line 601, in __getitem__
result = self.index.get_value(self, key)
File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2477, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas\_libs\index.pyx", line 98, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 759, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 765, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: -1
我使用python V3.6
我看到了兩個相同的答案哈哈:-) – Wen