對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只可用標籤索引取的行或者列。