iloc和loc的区别

对iloc和loc的不同,总是模棱两可,下面通过测试对比一下

import pandas as pd
import os
import numpy as np

设置文件夹

os.chdir("/Users/XXX/Documents/csv“)

打开文件、赋值并添加索引

In [8]:df = pd.read_csv("ex6.csv”)
In [9]: df1 = df.head(10)                                                       
In [10]: df1                                                                    
Out[10]: 
        one       two     three      four key
0  0.467976 -0.038649 -0.295344 -1.824726   L
1 -0.358893  1.404453  0.704965 -0.200638   B
2 -0.501840  0.659254 -0.421691 -0.057688   G
3  0.204886  1.074134  1.388361 -0.982404   R
4  0.354628 -0.133116  0.283763 -0.837063   Q
5  1.817480  0.742273  0.419395 -2.251035   Q
6 -0.776764  0.935518 -0.332872 -1.875641   U
7 -0.913135  1.530624 -0.572657  0.477252   K
8  0.358480 -0.497572 -0.367016  0.507702   S
9 -1.740877 -1.160417 -1.637830  2.172201   G
In [11]: df1.index = ['A','B','C','D','E','F','G','H','I','K’] 

iloc按照位置索引获取行数据,此为获取第零行的数据

In [14]: df1.iloc[0]                                                            
Out[14]: 
one       0.467976
two     -0.0386485
three    -0.295344
four      -1.82473
key              L
Name: A, dtype: object 

iloc按照位置索引获取第1行到第四行的数据,此为前闭后开

In [15]: df1.iloc[1:5]                                                          
Out[15]: 
        one       two     three      four key
B -0.358893  1.404453  0.704965 -0.200638   B
C -0.501840  0.659254 -0.421691 -0.057688   G
D  0.204886  1.074134  1.388361 -0.982404   R
E  0.354628 -0.133116  0.283763 -0.837063   Q   

下面表达式【16】、【17】中,iloc和loc的的含义一样,表示取的某几行,某几列的数据

In [16]: df1.iloc[[1,2],[2,3]]                                                  
Out[16]: 
      three      four
B  0.704965 -0.200638
C -0.421691 -0.057688
In [17]: df1.iloc[:,[2,3]]                                                      
Out[17]: 
      three      four
A -0.295344 -1.824726
B  0.704965 -0.200638
C -0.421691 -0.057688
D  1.388361 -0.982404
E  0.283763 -0.837063
F  0.419395 -2.251035
G -0.332872 -1.875641
H -0.572657  0.477252
I -0.367016  0.507702
K -1.637830  2.172201

loc为按标签索引取行或者列数据,此为取索引为’A’的行,此时也相当于取得第零行。

In [18]: df1.loc['A']                                                           
Out[18]: 
one       0.467976
two     -0.0386485
three    -0.295344
four      -1.82473
key              L
Name: A, dtype: object

此为按照行标签,取标签为’A’和标签为’C’的行。

In [19]: df1.loc[['A','C’]]
Out[19]: 
        one       two     three      four key
A  0.467976 -0.038649 -0.295344 -1.824726   L
C -0.501840  0.659254 -0.421691 -0.057688   G

In [20]: df1.loc[['A','C'],['one','three']]                            
Out[20]: 
        one     three
A  0.467976 -0.295344
C -0.501840 -0.421691

与【17】的的含义是一致的

In [21]: df1.loc[:,['one','three']]                                             
Out[21]: 
        one     three
A  0.467976 -0.295344
B -0.358893  0.704965
C -0.501840 -0.421691
D  0.204886  1.388361
E  0.354628  0.283763
F  1.817480  0.419395
G -0.776764 -0.332872
H -0.913135 -0.572657
I  0.358480 -0.367016
K -1.740877 -1.637830

下面测试下没有设着行索引的情况

In [44]: df2 = df.head(10)                                                      
In [45]: df2                                                                    
Out[45]: 
        one       two     three      four key
0  0.467976 -0.038649 -0.295344 -1.824726   L
1 -0.358893  1.404453  0.704965 -0.200638   B
2 -0.501840  0.659254 -0.421691 -0.057688   G
3  0.204886  1.074134  1.388361 -0.982404   R
4  0.354628 -0.133116  0.283763 -0.837063   Q
5  1.817480  0.742273  0.419395 -2.251035   Q
6 -0.776764  0.935518 -0.332872 -1.875641   U
7 -0.913135  1.530624 -0.572657  0.477252   K
8  0.358480 -0.497572 -0.367016  0.507702   S
9 -1.740877 -1.160417 -1.637830  2.172201   G

此时对于iloc没有大的改变,因为它是按位置获取的,从零开始的

In [46]: df2.iloc[1]                                                            
Out[46]: 
one     -0.358893
two       1.40445
three    0.704965
four    -0.200638
key             B
Name: 1, dtype: object

#iloc前闭后开

In [47]: df2.iloc[1:3]                                                          
Out[47]: 
        one       two     three      four key
1 -0.358893  1.404453  0.704965 -0.200638   B
2 -0.501840  0.659254 -0.421691 -0.057688   G

loc按照标签,所以相当于前闭后闭

In [48]: df2.loc[1:3]                                                           
Out[48]: 
        one       two     three      four key
1 -0.358893  1.404453  0.704965 -0.200638   B
2 -0.501840  0.659254 -0.421691 -0.057688   G
3  0.204886  1.074134  1.388361 -0.982404   R

这种取得和上面是一致得

In [49]: df2.loc[1:3,['one','three']]                                           
Out[49]: 
        one     three
1 -0.358893  0.704965
2 -0.501840 -0.421691
3  0.204886  1.388361
In [50]: df2.iloc[1:2:,1:2]                                                     
Out[50]: 
        two
1  1.404453

总结:iloc只可用位置索引取得行或者列。loc只可用标签索引取的行或者列。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章