在 Pandas 中以字符串形式获取索引标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23610577/
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-09-13 22:02:28  来源:igfitidea点击:

Get an index label as a string in Pandas

pandasindexing

提问by user3628698

I have a Pandas dataframe, the index/labels are date. I just want to get out the starting date (ie the first entry) and the ending date (ie the last entry). What is the best way to do that?

我有一个 Pandas 数据框,索引/标签是日期。我只想找出开始日期(即第一个条目)和结束日期(即最后一个条目)。最好的方法是什么?

Any help would be much appreciated.

任何帮助将非常感激。

回答by CONvid19

To get the indexof adataframeas a listuse:

indexadataframe作为list使用:

df.index.format()

回答by unutbu

You could use the index's format method. For example,

您可以使用索引的格式方法。例如,

In [44]: df = pd.DataFrame({'foo':1}, index=pd.date_range('2000-1-1', periods=5, freq='D'))

In [45]: df
Out[45]: 
            foo
2000-01-01    1
2000-01-02    1
2000-01-03    1
2000-01-04    1
2000-01-05    1

[5 rows x 1 columns]

In [46]: df.index[[0,-1]].format()
Out[46]: ['2000-01-01', '2000-01-05']