2014-10-02 5 views
1

팬더 0.14에서 멀티 인덱스를 찾는 방법을 찾을 수 없습니다. 다음은 문제가있는 일부 모의 데이터입니다.팬더 set_index 멀티 인덱스 검색

코드 :

row1 = ['red', 'ferrari', 'mine'] 
row2 = ['blue', 'ferrari', 'his'] 
row3 = ['red', 'lambo', 'his'] 
row4 = ['yellow', 'porsche', 'his'] 
row5 = ['yellow', 'lambo', 'his'] 
all_dat = [row1, row2, row3, row4, row5] 
df = DataFrame(all_dat, columns=['Color', 'Make', 'Ownership']) 

print df 
df = df.set_index(['Color', 'Make']) 
print df 

print df['red']['lambo'] 
print df['yellow']['porsche'] 

출력 :

Color  Make Ownership 
0  red ferrari  mine 
1 blue ferrari  his 
2  red lambo  his 
3 yellow porsche  his 
4 yellow lambo  his 
       Ownership 
Color Make    
red ferrari  mine 
blue ferrari  his 
red lambo   his 
yellow porsche  his 
     lambo   his 

Traceback (most recent call last): 
    print df['red']['lambo'] 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1678, in __getitem__ 
    return self._getitem_column(key) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1685, in _getitem_column 
    return self._get_item_cache(key) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1052, in _get_item_cache 
    values = self._data.get(item) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 2565, in get 
    loc = self.items.get_loc(item) 
    File "/usr/local/lib/python2.7/dist-packages/pandas/core/index.py", line 1181, in get_loc 
    return self._engine.get_loc(_values_from_object(key)) 
    File "index.pyx", line 129, in pandas.index.IndexEngine.get_loc (pandas/index.c:3354) 
    File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3234) 
    File "hashtable.pyx", line 696, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:11148) 
    File "hashtable.pyx", line 704, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:11101) 
KeyError: 'red' 

내가 시도 조회가

df[('red', 'lambo')] 

df['red', 'lambo'] 
를 사용하여

비슷한 결과 (KeyErrors)가 있습니다.

그래서 멀티 인덱스를 설정할 때 여기에 누락 된 단계가 있습니까? set_index()를 실제 데이터 (모의 데이터)로 사용하려면 인덱스를 다시 정의하기 전에 많은 작업을 수행해야합니다.

답변

2

df.loc 사용하여 튜플 목록으로 원하는 레이블을 지정할 수 있습니다

In [99]: df.loc[[('red','lambo')]] 
Out[99]: 
      Ownership 
Color Make   
red lambo  his 

In [106]: df.loc[[('yellow','porsche'), ('red','lambo')]] 
Out[106]: 
       Ownership 
Color Make    
yellow porsche  his 
red lambo   his 

할당은 다음과 같이 할 수있다 :

In [117]: df.loc[[('red', 'lambo')], 'Ownership'] = 'mine' 

In [118]: df 
Out[118]: 
       Ownership 
Color Make    
red ferrari  mine 
blue ferrari  his 
red lambo  mine 
yellow porsche  his 
     lambo   his 

항목 : Advanced indexing with hierarchical index

+0

감사합니다! 빠른 후속 조치를 통해 가치를 어떻게 업데이트 할 수 있습니까? 예를 들어 빨간 람보르기니 소유권을 '광산'으로 변경하는 것과 같습니다. 나는 'df.loc [[('red ','lambo ')]] ['Ownership '] ='mine''을 시도 중이다 – jab

+0

또한 멀티 인텍스를 검색하는 방법이 항상 있었습니까? 내 참조 자료의 일부에서'df [ 'red', 'lambo']'가 사용되고 있습니다. 팬더 API의 최근 변경 사항입니까? – jab

+0

알았어, 신경 쓰지 마. 나는 그것을 작동하게했다, 다시 한번 감사드립니다! – jab

관련 문제