[英]Pandas: Access row number
Such a simple question to which I can't find an answer: 这样一个简单的问题,我找不到答案:
In Pandas
, how do I access the row number of the index? 在
Pandas
,如何访问索引的行号?
The context is 上下文是
def func(row):
print row.col1,
# print row.index_value <--- What should go here?
return
df.apply(func, axis=1)
Thanks! 谢谢!
Use .name
attribute: 使用
.name
属性:
df = pd.DataFrame({'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'F':list('aaabbb')}).set_index('A')
print (df)
B C D E F
A
a 4 7 1 5 a
b 5 8 3 3 a
c 4 9 5 6 a
d 5 4 7 9 b
e 5 2 1 2 b
f 4 3 0 4 b
def func(row):
print (row.name)
return
a
b
c
d
e
f
df.apply(func, axis=1)
Close! 关! To access the index for a row it is simply:
要访问一行的索引,只需:
row.Index
Sorry, this is to be used when iterating through the rows of the dataframe using .itertuples()
. 抱歉,在使用
.itertuples()
遍历数据帧的行时将使用.itertuples()
。 Please see Jezrael's answer. 请参阅耶斯莱尔的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.