Skip to main content
 首页 » 编程设计

python之如何操作 pandas MultiIndex 切片中的字符串

2025年04月02日24lvdongjie

我有一个像这样的 MultiIndex:

                                 metric 
sensor  variable   side   
foo        Speed   Left      Left speed 
                  Right     Right speed 
bar        Speed   Left      Left_Speed 
                  Right     Right_Speed 
baz        Speed   Left           speed 
foo      Support   Left    Left support 
                  Right   Right support 
bar      Support   Left    Left_support 
                  Right   Right_support 
baz      Support   Left         support 

我正在尝试将字符串映射应用于此数据帧的一部分:

df.loc['baz',:,'Left'].metric.map(lambda s: "Left_" + s) 

我如何才能将此映射仅应用于 baz-Left 行,并取回生成的 DataFrame?

                                 metric 
sensor  variable   side   
foo        Speed   Left      Left speed 
                  Right     Right speed 
bar        Speed   Left      Left_Speed 
                  Right     Right_Speed 
baz        Speed   Left      Left_speed 
foo      Support   Left    Left support 
                  Right   Right support 
bar      Support   Left    Left_support 
                  Right   Right_support 
baz      Support   Left    Left_support 

请您参考如下方法:

我找到了以下方法,但我认为/希望一定有更优雅的方法来实现:

In [101]: index_saved = df.index 

让我们对索引进行排序以消除 KeyError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (3), lexsort depth (0)' 错误:

In [102]: df = df.sort_index() 
 
In [103]: df 
Out[103]: 
                              metric 
sensor variable side 
bar    Speed    Left      Left_Speed 
                Right    Right_Speed 
       Support  Left    Left_support 
                Right  Right_support 
baz    Speed    Left           speed 
       Support  Left         support 
foo    Speed    Left      Left speed 
                Right    Right speed 
       Support  Left    Left support 
                Right  Right support 
 
In [119]: df.loc[pd.IndexSlice['baz', :, 'Left'], 'metric'] = \ 
     ...:     'AAA__' + df.loc[pd.IndexSlice['baz', :, 'Left'], 'metric'] 
 
In [120]: df 
Out[120]: 
                              metric 
sensor variable side 
bar    Speed    Left      Left_Speed 
                Right    Right_Speed 
       Support  Left    Left_support 
                Right  Right_support 
baz    Speed    Left      AAA__speed 
       Support  Left    AAA__support 
foo    Speed    Left      Left speed 
                Right    Right speed 
       Support  Left    Left support 
                Right  Right support 

设置旧的(保存的)索引:

In [121]: df = df.reindex(index_saved) 
 
In [122]: df 
Out[122]: 
                              metric 
sensor variable side 
foo    Speed    Left      Left speed 
                Right    Right speed 
bar    Speed    Left      Left_Speed 
                Right    Right_Speed 
baz    Speed    Left      AAA__speed 
foo    Support  Left    Left support 
                Right  Right support 
bar    Support  Left    Left_support 
                Right  Right_support 
baz    Support  Left    AAA__support