登录 |  注册 |  繁體中文


pandas 中行索引 loc和iloc的区别

分类: python 颜色:橙色 默认  字号: 阅读(583) | 评论(0)

loc是指location的意思,iloc中的i是指integer。二者都是行索引

这两者的区别如下:

loc:works on labels in the index.

iloc:works on the positions in the index (so it only takes integers).

也就是说loc是根据index来索引,比如下边的df定义了一个index,那么loc就根据这个index来索引对应的行。

iloc根据行号来索引,行号从0开始,逐次加1。

In [1]: df = DataFrame(randn(5,2),index=range(0,10,2),columns=list(AB))

In [2]: df
Out[2]: 
          A         B
0  1.068932 -0.794307
2 -0.470056  1.192211
4 -0.284561  0.756029
6  1.037563 -0.267820
8 -0.538478 -0.800654

In [5]: df.iloc[[2]]
Out[5]: 
          A         B
4 -0.284561  0.756029

In [6]: df.loc[[2]]
Out[6]: 
          A         B
2 -0.470056  1.192211


上一篇:图像二值化   下一篇:Pandas 的常见使用方法

姓 名: *
邮 箱:
内 容: *
验证码: 点击刷新 *   

回到顶部