2016-06-09 4 views
2

의해 정의 dataframe하는 그때 제가할당 값 multiindex

slice = results.loc['factor2'].copy() 

을 수행하고 (함수에서 수행) 슬라이스에 계산 된 값을 기입

factor_list = ['factor1', 'factor2', 'factor3'] 
method_list = ['method1', 'method2', 'method3'] 

grouping_list = ['group1', 'group2', 'group3'] 

parameter_list = [1, 5, 10, 20, 40] 

iterables = [factor_list, method_list, parameter_list, grouping_list] 

axis_names = ['factor', 'method', 'param', 'grouping'] 

multi_index = pd.MultiIndex.from_product(iterables, names=axis_names) 

column_list = ['a', 'b', 'c', 'd', 'e'] 

results = pd.DataFrame(index=multi_index, columns=column_list) 

results.sort_index(inplace=True) 

의해 만들어진 5 차원 DF있다.

cannot align on a multi-index with out specifying the join levels 

정확한 질문은 다음과 같습니다 :
어떻게 다시 조각의 콘텐츠를 복사 할
그럼 내가 다음 줄은 오류를 발생시킵니다 다시

results.loc['factor2'] = slice 

결과를 복사 할 수 없습니다 것을 발견 factor2 일부 결과 DataFrame? 나를 위해

+1

몇 가지 예제를 게시에 대해 마스크 할 수 있습니까? – WoodChopper

답변

2

locusing slicers 작동 : 필요한 경우

import pandas as pd, numpy as np 

factor_list = ['factor1', 'factor2', 'factor3'] 
method_list = ['method1', 'method2', 'method3'] 

grouping_list = ['group1', 'group2', 'group3'] 
strategy_list = ['s1', 's2', 's3'] 

parameter_list = [1, 5, 10, 20, 40] # -999 mean not applicable 

iterables = [factor_list, strategy_list, parameter_list, grouping_list] 

axis_names = ['factor', 'method', 'param', 'grouping'] 

multi_index = pd.MultiIndex.from_product(iterables, names=axis_names) 

column_list = ['a', 'b', 'c', 'd', 'e'] 

results = pd.DataFrame(np.arange(len(multi_index)*len(column_list)).reshape((len(multi_index),len(column_list))), 
         index=multi_index, columns=column_list) 
results.sort_index(inplace=True) 
print (results.head()) 
           a b c d e 
factor method param grouping      
factor1 s1  1  group1  0 1 2 3 4 
        group2  5 6 7 8 9 
        group3 10 11 12 13 14 
       5  group1 15 16 17 18 19 
        group2 20 21 22 23 24 

idx = pd.IndexSlice 
sli = results.loc[idx['factor1',:,:,['group1','group3']],:] 

#some test function 
sli = sli + 0.1 
print (sli.head()) 
            a  b  c  d  e 
factor method param grouping        
factor1 s1  1  group1  0.1 1.1 2.1 3.1 4.1 
        group3 10.1 11.1 12.1 13.1 14.1 
       5  group1 15.1 16.1 17.1 18.1 19.1 
        group3 25.1 26.1 27.1 28.1 29.1 
       10 group1 30.1 31.1 32.1 33.1 34.1 

results.loc[idx['factor1',:,:,['group1','group3']],:] = sli 
print (results.head()) 
            a  b  c  d  e 
factor method param grouping        
factor1 s1  1  group1  0.1 1.1 2.1 3.1 4.1 
        group2  5.0 6.0 7.0 8.0 9.0 
        group3 10.1 11.1 12.1 13.1 14.1 
       5  group1 15.1 16.1 17.1 18.1 19.1 
        group2 20.0 21.0 22.0 23.0 24.0 
+0

감사합니다. 결과를 통해 나 자신도 알아 냈습니다 .loc [ 'factor2']. loc [slice.index] = slice – wh408

+0

수락 해 주셔서 감사합니다. 나는 첫 번째 버전의 [script] (http://stackoverflow.com/revisions/37724208/5)를'column_list'에 이중'b' 열로 테스트하고 실제로 작동하지 않으며 오랜 시간 문제를 찾을 수 없다.). 좋은 날! – jezrael