繁体   English   中英

熊猫:访问行号

[英]Pandas: Access row number

这样一个简单的问题,我找不到答案:

Pandas ,如何访问索引的行号?

上下文是

def func(row):
    print row.col1,
    # print row.index_value <--- What should go here?
    return

df.apply(func, axis=1)

谢谢!

使用.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)

关! 要访问一行的索引,只需:

row.Index

抱歉,在使用.itertuples()遍历数据帧的行时将使用.itertuples() 请参阅耶斯莱尔的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM