为多索引定义的数据帧赋值



>我有一个 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

确切的问题是:
如何将切片中的内容复制回结果DataFramefactor2部分?

对我来说,

使用切片器与loc

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

相关内容

  • 没有找到相关文章

最新更新