2013-01-03 4 views
12

두 개의 데이터 프레임이 있습니다. DF1은 멀티 - 색인입니다 :팬더에서 단일 색인 데이터 프레임과 다중 색인 병합

   value 
first second  
a  x   0.471780 
     y   0.774908 
     z   0.563634 
b  x   -0.353756 
     y   0.368062 
     z   -1.721840 

및 DF2 :

 value 
first 
a  10 
b  20 

가 어떻게이 케이스 '첫 번째'인덱스, 멀티 인덱스의 하나를 사용하여 두 개의 데이터 프레임을 병합 할 수 있습니다?

firsts = df1.index.get_level_values('first') 
df1['value2'] = df2.ix[firsts].values 

참고 :

   value1  value2 
first second  
a  x   0.471780 10 
     y   0.774908 10 
     z   0.563634 10 
b  x   -0.353756 20 
     y   0.368062 20 
     z   -1.721840 20 

답변

9

당신은 get_level_values을 사용할 수 원하는 출력이 될 것입니다 당신이 거의합니다 (DF1입니다 MultiIndex 제외) 여기에 join을하고 있습니다 ... 그래서이있을 수 있습니다 이것을 설명하는 더 좋은 방법은 ...

. (당신이 가진 것과 비슷한) 예에서

:

df1 = pd.DataFrame([['a', 'x', 0.123], ['a','x', 0.234], 
        ['a', 'y', 0.451], ['b', 'x', 0.453]], 
        columns=['first', 'second', 'value1'] 
        ).set_index(['first', 'second']) 
df2 = pd.DataFrame([['a', 10],['b', 20]], 
        columns=['first', 'value']).set_index(['first']) 

firsts = df1.index.get_level_values('first') 
df1['value2'] = df2.ix[firsts].values 

In [5]: df1 
Out[5]: 
       value1 value2 
first second     
a  x  0.123  10 
     x  0.234  10 
     y  0.451  10 
b  x  0.453  20 
+0

할 수 있습니다 * 거의 * 다음과 같이 병합 :'df1.merge (DF2, left_on = df1.index.get_level_values ​​() '첫 번째', right_on = df2.index.get_level_values ​​('첫 번째'))' –

2

.ix 구문은 다시 색인하는 강력한 바로 가기이지만,이 경우에 당신이 실제로 결합 행/열 인덱싱을 수행하지 않는,이를 그런 다음

df1 = pd.DataFrame([['a', 'x', 0.123], ['a','x', 0.234], 
        ['a', 'y', 0.451], ['b', 'x', 0.453]], 
        columns=['first', 'second', 'value1'] 
        ).set_index(['first', 'second']) 
df2 = pd.DataFrame([['a', 10],['b', 20]], 
        columns=['first', 'value']).set_index(['first']) 

: 헤이든에서

준비 : 단지 사용하여 인덱싱과 (내 겸손 입맛을 위해) 더 우아하게 비트를 수행 할 수 있습니다 이 iPython에서 다음과 같습니다

In [4]: df1 
Out[4]: 
       value1 
first second   
a  x  0.123 
     x  0.234 
     y  0.451 
b  x  0.453 

In [5]: df2 
Out[5]: 
     value 
first  
a   10 
b   20 

In [7]: df2.reindex(df1.index, level=0) 
Out[7]: 
       value 
first second  
a  x   10 
     x   10 
     y   10 
b  x   20 

In [8]: df1['value2'] = df2.reindex(df1.index, level=0) 

In [9]: df1 
Out[9]: 
       value1 value2 
first second     
a  x  0.123  10 
     x  0.234  10 
     y  0.451  10 
b  x  0.453  20 

당신이 재 인덱싱 방법에 사용해야 어떤 수준의 mnemotechnic : 그것은 당신이 이미 더 큰 인덱스에서 다루는 수준 상태. 그래서이 경우 df2는 이미 df1.index의 레벨 0을가집니다.

4

the documentation에 따르면 팬더 0.14부터는 단일 색인 및 다중 색인 데이터 프레임에 간단하게 참여할 수 있습니다. 공통 색인 이름과 일치합니다. 흥미롭게도 'left''right' (이것은 버그 일 수 있겠습니까?)에 대해 반전 된 것으로 보이지만 how 인수는 'inner''outer'으로 예상대로 작동합니다.

df1 = pd.DataFrame([['a', 'x', 0.471780], ['a','y', 0.774908], ['a', 'z', 0.563634], 
        ['b', 'x', -0.353756], ['b', 'y', 0.368062], ['b', 'z', -1.721840], 
        ['c', 'x', 1], ['c', 'y', 2], ['c', 'z', 3], 
        ], 
        columns=['first', 'second', 'value1'] 
        ).set_index(['first', 'second']) 
df2 = pd.DataFrame([['a', 10], ['b', 20]], 
        columns=['first', 'value2']).set_index(['first']) 

print(df1.join(df2, how='inner')) 
       value1 value2 
first second     
a  x  0.471780  10 
     y  0.774908  10 
     z  0.563634  10 
b  x  -0.353756  20 
     y  0.368062  20 
     z  -1.721840  20