pandas DataFrame에서 index 통해 해당하는 row 찾을 사용하는 .iloc, .loc, .ix 겉보기에 다르지 않지만 각각의 용도가 다르다고 한다.

.iloc

integer positon 통해 값을 찾을 있다. label로는 찾을 없다

.loc

label 통해 값을 찾을 있다. integer position로는 찾을 없다.

.ix

integer position label모두 사용 있다. 만약 label 숫자라면 label-based index 된다.

 

예제

다음과 같이 label 숫자로 이루어진 Series 있다면,

>>> 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


s.iloc[:3]
integer position 이용하므로 1-3번째 row 반환할 것이고s.loc[:3] label 이용하므로 1-8번째 row 반환할 이다s.ix[:3] label 숫자인경우 label-based indexing 따르므로 .loc 처럼 1-8번째 row 반환할 것이다.

하지만, 만일 label 아래와같이 숫자와 문자로 이루어져 있다면,

>>> s2 = pd.Series(np.nan, index=['a','b','c','d','e', 1, 2, 3, 4, 5])

>>> s2.index.is_mixed() # index is mix of types

True

>>> s2.ix[:6] # behaves like iloc given integer

a   NaN

b   NaN

c   NaN

d   NaN

e   NaN

1   NaN


s2.ix[:6]
에러를 내지 않고 iloc처럼 1-6번째 row 반환한다. 또한 s2.ix[:'c'] 하면 loc처럼 1-3번째 row 반환한다.

만일 label 이용하던지, 아니면 integer position 이용하던지 하나만 가지고 indexing하고자 한다면 iloc이나 loc 하나를 사용하면 된다. 하지만 방법을 섞어서 쓰고 싶다면 반드시 ix 사용해야한다. 예를 들면 다음과 같다.

>>> df = pd.DataFrame(np.arange(25).reshape(5,5),

                      index=list('abcde'),

                      columns=['x','y','z', 8, 9])

>>> df

    x   y   z   8   9

a   0   1   2   3   4

b   5   6   7   8   9

c  10  11  12  13  14

d  15  16  17  18  19

e  20  21  22  23  24


mixed index
사용하고 있는 DataFrame, df 'c' x '8' 컬럼만 slicing하고자 한다면 ix 통해서 다음과 같이 있다.

>>> df.ix[:'c', :4]

    x   y   z   8

a   0   1   2   3

b   5   6   7   8

c  10  11  12  13

 


Posted by roselumi
,