如何从系列中获取数据框索引? [英] How to get dataframe index from series?
本文介绍了如何从系列中获取数据框索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我从数据框中提取一个系列(就像应用函数会发生的那样)。我正在尝试从该系列中找到原始数据框索引。
Say I extract a series from a dataframe (like what would happen with an apply function). I'm trying to find the original dataframe index from that series.
df=pd.DataFrame({'a':[1,2,3],'b':[4,5,6],'c':[7,8,9]})
x=df.ix[0]
x
Out[109]:
a 1
b 4
c 7
Name: 0, dtype: int64
注意输出底部的Name:0部分。如何从系列对象x获取值'?
Notice the "Name: 0" piece at the bottom of the output. How can I get the value '0' from series object x?
推荐答案
您使用访问它姓名
属性
x.name
0
采取这些例子
for i, row in df.iterrows():
print row.name, i
0 0
1 1
2 2
请注意,name属性与变量 i
相同,这是应该是行索引。
Notice that the name attribute is the same as the variable i
which is supposed to be the row index.
列是相同的
for j, col in df.iteritems():
print col.name, j
a a
b b
c c
这篇关于如何从系列中获取数据框索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文