Python 获取数据帧的最后一个值';带索引的s列

Python 获取数据帧的最后一个值';带索引的s列,python,pandas,Python,Pandas,我试图获取名为“mass”的列的最后一个值 这引起了一个错误: Traceback (most recent call last): File "plot.py", line 22, in <module> df2=df['mass'][-1] File "/usr/lib64/python2.7/site-packages/pandas/core/series.py", line 557, in __getitem__

我试图获取名为“mass”的列的最后一个值

这引起了一个错误:

Traceback (most recent call last):
  File "plot.py", line 22, in <module>
    df2=df['mass'][-1]
  File "/usr/lib64/python2.7/site-packages/pandas/core/series.py", line 557, in __getitem__
    result = self.index.get_value(self, key)
  File "/usr/lib64/python2.7/site-packages/pandas/core/index.py", line 1790, in get_value
    return self._engine.get_value(s, k)
  File "pandas/index.pyx", line 103, in pandas.index.IndexEngine.get_value (pandas/index.c:3204)
  File "pandas/index.pyx", line 111, in pandas.index.IndexEngine.get_value (pandas/index.c:2903)
  File "pandas/index.pyx", line 157, in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)
  File "pandas/hashtable.pyx", line 303, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)
  File "pandas/hashtable.pyx", line 309, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)
KeyError: -1
回溯(最近一次呼叫最后一次):
文件“plot.py”,第22行,在
df2=df[‘质量’][-1]
文件“/usr/lib64/python2.7/site packages/pandas/core/series.py”,第557行,在__
结果=self.index.get_值(self,key)
文件“/usr/lib64/python2.7/site packages/pandas/core/index.py”,第1790行,在get_值中
返回self.\u引擎。获取值(s,k)
pandas.index.IndexEngine.get_值(pandas/index.c:3204)中第103行的文件“pandas/index.pyx”
pandas.index.IndexEngine.get_值(pandas/index.c:2903)中的文件“pandas/index.pyx”,第111行
pandas.index.IndexEngine.get_loc(pandas/index.c:3843)中的文件“pandas/index.pyx”,第157行
pandas.hashtable.Int64HashTable.get_项(pandas/hashtable.c:6525)中第303行的文件“pandas/hashtable.pyx”
pandas.hashtable.Int64HashTable.get_项(pandas/hashtable.c:6463)中第309行的文件“pandas/hashtable.pyx”
键错误:-1

但是,我使用
df['mass'][0]
获得了第一个值。为什么它不适用于索引“-1”?我知道有一些替代方法,比如使用
iloc
iat
值。但是我认为如果它能工作的话,
df['mass'][0]
将是最简单的方法。

df['mass']
返回一个序列,因此使用
[]
进一步选择是基于标签的索引选择。您的索引没有标签
-1
,因此会出现该错误。您希望基于数组索引的选择使用
-1
选择最后一个值,这是通过
iloc
实现的:
df['mass'].iloc[-1]
感谢您的回答。这是因为我对pandas缺乏了解。为了完整起见,
df['mass'][0]
之所以有效,唯一的原因是pandas默认创建的数据帧的范围索引从
0
开始,因此
0
恰好是第一行的标签。但是如果您的索引是
['a','b','c',…]
df['mass'][0]
将遇到相同的问题,而
df['mass'].iloc[0]
将成功选择第一个值。
Traceback (most recent call last):
  File "plot.py", line 22, in <module>
    df2=df['mass'][-1]
  File "/usr/lib64/python2.7/site-packages/pandas/core/series.py", line 557, in __getitem__
    result = self.index.get_value(self, key)
  File "/usr/lib64/python2.7/site-packages/pandas/core/index.py", line 1790, in get_value
    return self._engine.get_value(s, k)
  File "pandas/index.pyx", line 103, in pandas.index.IndexEngine.get_value (pandas/index.c:3204)
  File "pandas/index.pyx", line 111, in pandas.index.IndexEngine.get_value (pandas/index.c:2903)
  File "pandas/index.pyx", line 157, in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)
  File "pandas/hashtable.pyx", line 303, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)
  File "pandas/hashtable.pyx", line 309, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)
KeyError: -1