我有一个叫做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