**
這篇部落格
直接看例子:
>>> data = pd.series
(np.arange(10), index=[49,48,47,46,45, 1, 2, 3, 4, 5])
>>> data490
481472
463454
1526
3748
59dtype: int64
>>> data.iloc[:3]490
481472
dtype: int64
>>> data.loc[:3]490
481472
463454
1526
37dtype: int64
>>> data.ix[:3]490
481472
463454
1526
37dtype: int64
解析:
loc 在index的標籤上進行索引,範圍包括start和end.
iloc 在index的位置上進行索引,不包括end.
ix 先在index的標籤上索引,索引不到就在index的位置上索引(如果index非全整數),不包括end.
>>> data.iloc[:6]490
481472
463454
15dtype: int64
>>> data.loc[:6]
keyerror:
6>>> data.ix[:6] #因為index裡面不包含標籤6,index都是整數
keyerror:
6>>> data= pd.series(np.arange(10), index=['a','b','c','d','e', 1, 2, 3, 4, 5])
>>> data
a 0
b 1
c 2
d 3
e 415
2637
4859
dtype: int64
>>> data.ix[:6]
a 0
b 1
c 2
d 3
e 415
dtype: int64
>>> data.loc[:6]
typeerror: cannot do slice indexing
建議: 為了避免歧義,建議優先選擇loc和iloc pandas中loc, iloc和ix的區別
loc 通過行標籤索引資料 iloc 通過行號索引資料 ix 可通過行號索引資料也可通過標籤索引資料 例項 實際資料如下所示,dataframe中第一行資料為1,2,3,行號為0,行標籤為a,也就是我們說的索引名稱為a,第二行同理。現在我們只需要訪問第一行資料,通過loc,iloc,ix的方式進行訪...
pandas資料索引 loc iloc和ix
1 loc 通過行標籤索引行資料 1 loc d 獲取第 d 行資料 import pandas as pd data 1,2,3 4,5,6 index d e columns a b c df pd.dataframe data data,index index,columns columns ...
Pandas中loc,iloc與直接切片的區別
0.把series的行index或dataframe的列名直接當做屬性來索引。如 s.index name df.column name 但是這種方法索引的名字可能會與自帶的方法衝突,比如min,max等等,所以可能會失效。另外,在新版本中,這種索引方法不能作為左值。1.df直接索引 直接索引索引的...