Pandas 获取给定索引的系列的“索引”标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33929467/
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
Pandas get the "index" label of a series given an index
提问by Christopher
Ok, so this is confusing because of a lack of vocabulary.
好吧,由于缺乏词汇,这很令人困惑。
Pandas series have an index and a value: so 'series[0]' contains (index,value).
Pandas 系列有一个索引和一个值:所以 'series[0]' 包含 (index,value)。
How do I get the index (in my case it is a date), out of the series by indexing the series? This is really a very simple idea...it is just encrypted by the word "index." lol.
如何通过索引系列从系列中获取索引(在我的情况下是日期)?这真的是一个非常简单的想法……它只是用“索引”这个词加密了。哈哈。
So, to rephrase,
所以,换个说法,
I need the date of the first entry in my series and the last entry, when my series is indexed by date.
当我的系列按日期索引时,我需要系列中第一个条目和最后一个条目的日期。
just to be clear, I have a series indexed by date, so when I print it out, it prints:
为了清楚起见,我有一个按日期索引的系列,所以当我打印出来时,它会打印:
12-12-2008 1.2
12-13-2008 1.3
...
and calling
并打电话
df.ix[0] -> 1.2
I need:
我需要:
df.something[0] -> 12-12-2008
采纳答案by Christopher
Got it.
知道了。
df.index[0]
yields the label at index 0.
在索引 0 处产生标签。
回答by maxymoo
You can access the elements of your index just as you would a list. So df.index[0]
will be the first element of your index and df.index[-1]
will be the last.
您可以像访问列表一样访问索引的元素。所以df.index[0]
将是索引的第一个元素,也df.index[-1]
将是最后一个。
Incidently if a series (or dataframe) has a non-integer index, df.ix[n]
will return the n
-th row corresponding to the n
-th element of your index.
顺便说一句,如果一个系列(或数据帧)有一个非整数索引,df.ix[n]
将返回与索引的n
第n
-th 个元素相对应的第-th 行。
So df.ix[0]
will return the first row and df.ix[-1]
will return the last row. So an alternative way of getting the index values would be to use df.ix[0].name
and df.ix[-1].name
所以df.ix[0]
将返回第一行并df.ix[-1]
返回最后一行。因此,获取索引值的另一种方法是使用df.ix[0].name
和df.ix[-1].name