Python 访问数据帧的最后一个索引值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32341797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:24:29  来源:igfitidea点击:

Access last index value of dataframe

pythonpandas

提问by Stacey

I have an example dataframe (df) for 2 products:

我有 2 个产品的示例数据框 (df):

                         BBG.XAMS.FUR.S               BBG.XAMS.MT.S
date                                                               
2014-10-23                 -2368.850388                    0.000000
2014-10-24                  6043.456178                    0.000000
2015-03-23                    -0.674996                   -0.674997
2015-03-24                    82.704951                   11.868748
2015-03-25                   -11.027327                   84.160210

Is there a way to retrieve the last index value of a dataframe only. So in this example the date value I need retrieved is 2015-03-25?

有没有办法只检索数据帧的最后一个索引值。所以在这个例子中,我需要检索的日期值是 2015-03-25?

采纳答案by EdChum

The following gives you the last index value:

以下为您提供最后一个索引值:

df.index[-1]

Example:

例子:

In [37]:

df.index[-1]
Out[37]:
Timestamp('2015-03-25 00:00:00')

Or you could access the index attribute of the tail:

或者您可以访问以下项的 index 属性tail

In [40]:

df.tail(1).index[0]
Out[40]:
Timestamp('2015-03-25 00:00:00')

回答by dsugasa

Old post, but df.last_valid_index()also works.

旧帖子,但df.last_valid_index()也有效。