将值分配给multiindex定义的数据帧

时间:2016-06-09 11:05:52

标签: python pandas dataframe multi-index

我有一个由

创建的5维df
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)

然后我做

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

并将计算值填入切片(由函数完成) 然后我发现我无法将结果复制回来

results.loc['factor2'] = slice 

以下行引发错误:

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

确切的问题是:
如何将切片中的内容复制回结果factor2的{​​{1}}部分?

1 个答案:

答案 0 :(得分:2)

对我而言using slicersloc合作:

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