首先,介紹這三種方法的概述:
loc: loc gets rows (or columns) with particular labels from the index. loc從索引中獲取具有特定標簽的行(或列)。這里的關鍵是:標簽。標簽的理解就是name名字。
iloc: gets rows (or columns) at particular positions in the index (so it only takes integers). iloc在索引中的特定位置獲取行(或列)(因此它只接受整數)。這里的關鍵是:位置。位置的理解就是排第幾個。
ix: usually tries to behave like loc but falls back to behaving like iloc if a label is not present in the index. ix通常會嘗試像loc一樣行為,但如果索引中不存在標簽,則會退回到像iloc一樣的行為。
1.loc
其實,對於loc始終堅持一個原則:loc是基於label進行索引的!
import pandas as pd df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c']) df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c']) print(df1) print(df2) ''' df1: a b c 0 1 2 3 1 4 5 6 2 7 8 9 df2: a b c e 1 2 3 f 4 5 6 g 7 8 9 ''' # loc索引行,label是整型數字 print(df1.loc[0]) ''' a 1 b 2 c 3 Name: 0, dtype: int64 ''' # loc索引行,label是字符型 print(df2.loc['e']) ''' a 1 b 2 c 3 Name: 0, dtype: int64 '''
# 如果對df2這么寫:df2.loc[0]會報錯,因為loc索引的是label,顯然在df2的行的名字中沒有叫0的。 print(df2.loc[0]) ''' TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'> ''' # loc索引多行數據 print(df1.loc[1:]) ''' a b c 1 4 5 6 2 7 8 9 ''' # loc索引多列數據 print(df1.loc[:,['a', 'b']]) ''' a b 0 1 2 1 4 5 2 7 8 '''
# df1.loc[:,0:2]這么寫報錯, 因為loc索引的是label,顯然在df1的列的名字中沒有叫0,1和2的。 print(df1.loc[:,0:2]) ''' TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'> ''' # locs索引某些行某些列 print(df1.loc[0:2, ['a', 'b']]) ''' a b 0 1 2 1 4 5 2 7 8 '''
2.iloc
對於iloc始終也堅持一個原則:iloc是基於position進行索引的!
import pandas as pd df1 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=[0, 1, 2], columns=['a','b','c']) df2 = pd.DataFrame(data= [[1, 2, 3],[4, 5, 6], [7, 8, 9]], index=['e', 'f', 'g'], columns=['a','b','c']) print(df1) print(df2) ''' df1: a b c 0 1 2 3 1 4 5 6 2 7 8 9 df2: a b c e 1 2 3 f 4 5 6 g 7 8 9 '''
# iloc索引行,label是整型數字 print(df1.iloc[0]) ''' a 1 b 2 c 3 Name: 0, dtype: int64 ''' # iloc索引行,label是字符型。如果按照loc的寫法來寫應該是:df2.iloc['e'],顯然這樣報錯,因為iloc不認識label,它是基於位置的。 print(df2.iloc['e']) ''' TypeError: cannot do positional indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [e] of <class 'str'> '''
# iloc索引行,label是字符型。正確的寫法應該如下: # 也就說,不論index是什么類型的,iloc只能寫位置,也就是整型數字。 print(df2.iloc[0]) ''' a 1 b 2 c 3 Name: e, dtype: int64 ''' # iloc索引多行數據 print(df1.iloc[1:]) ''' a b c 1 4 5 6 2 7 8 9 ''' # iloc索引多列數據 # 如果如下寫法,報錯。 print(df1.iloc[:,['a', 'b']]) ''' TypeError: cannot perform reduce with flexible type '''
# iloc索引多列數據, 正確寫法如下: print(df1.iloc[:,0:2]) ''' a b 0 1 2 1 4 5 2 7 8 ''' # iloc索引某些行某些列 print(df1.iloc[0:2, 0:1]) ''' a 0 1 1 4 '''
3.ix
注:ix的操作比較復雜,在pandas版本0.20.0及其以后版本中,ix已經不被推薦使用,建議采用iloc和loc實現ix。
(1)如果索引是整數類型,則ix將僅使用基於標簽的索引,而不會回退到基於位置的索引。如果標簽不在索引中,則會引發錯誤。
(2)如果索引不僅包含整數,則給定一個整數,ix將立即使用基於位置的索引而不是基於標簽的索引。但是,如果ix被賦予另一種類型(例如字符串),則它可以使用基於標簽的索引。
接下來舉例說明這兩個特點:
>>> s = pd.Series(np.nan, index=[49,48,47,46,45, 1, 2, 3, 4, 5]) >>> s 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN 4 NaN 5 NaN
現在我們來看使用整數3切片有什么結果:
在這個例子中,s.iloc[:3]讀取前3行(因為iloc把3看成是位置position),而s.loc[:3]讀取的是前8行(因為loc把3看作是索引的標簽label)
>>> s.iloc[:3] # slice the first three rows 49 NaN 48 NaN 47 NaN >>> s.loc[:3] # slice up to and including label 3 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN >>> s.ix[:3] # the integer is in the index so s.ix[:3] works like loc 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN
注意:s.ix[:3]返回的結果與s.loc[:3]一樣,這是因為如果series的索引是整型的話,ix會首先去尋找索引中的標簽3而不是去找位置3。
如果,我們試圖去找一個不在索引中的標簽,比如說是6呢?
>>> s.iloc[:6] 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN >>> s.loc[:6] KeyError: 6 >>> s.ix[:6] KeyError: 6
在上面的例子中,s.iloc[:6]正如我們所期望的,返回了前6行。而,s.loc[:6]返回了KeyError錯誤,這是因為標簽6並不在索引中。
那么,s.ix[:6]報錯的原因是什么呢?正如我們在ix的特點1所說的那樣,如果索引只有整數類型,那么ix僅使用基於標簽的索引,而不會回退到基於位置的索引。如果標簽不在索引中,則會引發錯誤。
如果我們的索引是一個混合的類型,即不僅僅包括整型,也包括其他類型,如字符類型。那么,給ix一個整型數字,ix會立即使用iloc操作,而不是報KeyError錯誤。
>>> s2 = pd.Series(np.nan, index=['a','b','c','d','e', 1, 2, 3, 4, 5]) >>> s2.index.is_mixed() # index is mix of different types True >>> s2.ix[:6] # now behaves like iloc given integer a NaN b NaN c NaN d NaN e NaN 1 NaN
注意:在這種情況下,ix也可以接受非整型,這樣就是loc的操作:
>>> s2.ix[:'c'] # behaves like loc given non-integer a NaN b NaN c NaN
這個例子就說明了ix特點2。
正如前面所介紹的,ix的使用有些復雜。如果僅使用位置或者標簽進行切片,使用iloc或者loc就行了,請避免使用ix。
4.在Dataframe中使用ix實現復雜切片
有時候,在使用Dataframe進行切片時,我們想混合使用標簽和位置來對行和列進行切片。那么,應該怎么操作呢?
舉例,考慮有下述例子中的Dataframe。我們想得到直到包含標簽'c'的行和前4列。
>>> df = pd.DataFrame(np.nan, index=list('abcde'), columns=['x','y','z', 8, 9]) >>> df x y z 8 9 a NaN NaN NaN NaN NaN b NaN NaN NaN NaN NaN c NaN NaN NaN NaN NaN d NaN NaN NaN NaN NaN e NaN NaN NaN NaN NaN
在pandas的早期版本(0.20.0)之前,ix可以很好地實現這個功能。
我們可以使用標簽來切分行,使用位置來切分列(請注意:因為4並不是列的名字,因為ix在列上是使用的iloc)。
>>> df.ix[:'c', :4] x y z 8 a NaN NaN NaN NaN b NaN NaN NaN NaN c NaN NaN NaN NaN
在pandas的后來版本中,我們可以使用iloc和其它的一個方法就可以實現上述功能:
>>> df.iloc[:df.index.get_loc('c') + 1, :4] x y z 8 a NaN NaN NaN NaN b NaN NaN NaN NaN c NaN NaN NaN NaN
get_loc()
是得到標簽在索引中的位置的方法。請注意,因為使用iloc切片時不包括最后1個點,因為我們必須加1。
可以看到,只使用iloc更好用,因為不必理會ix的那2個“繁瑣”的特點。
轉載於https://blog.csdn.net/anshuai_aw1/article/details/82801435