How can I manipulate strings in a slice of a pandas MultiIndex
<p>I have a <code>MultiIndex</code> like this:</p>

<pre><code>                                 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
</code></pre>

<p>I'm trying to apply a string mapping to a slice of this dataframe:</p>

<pre><code>df.loc['baz',:,'Left'].metric.map(lambda s: "Left_" + s)
</code></pre>

<p>How can I apply this map to just the <code>baz-Left</code> rows, and get back the resulting <code>DataFrame</code>?</p>

<pre><code>                                 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
</code></pre>

<p>I found the following method, but i think/hope there must be a more elegant way to achieve that:</p>

<pre><code>In [101]: index_saved = df.index
</code></pre>

<p>Let's sort index in order to get rid of <code>KeyError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (3), lexsort depth (0)'</code> error:</p>

<pre><code>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
</code></pre>

<p>set back old (saved) index:</p>

<pre><code>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
</code></pre>

python pandas