title: Applying a function to a MultiIndex pandas.DataFrame column
body: <p>I have a MultiIndex pandas DataFrame in which I want to apply a function to one of its columns and assign the result to that same column.</p>
  
  <pre class="lang-py prettyprint-override"><code>In [1]:
      import numpy as np
      import pandas as pd
      cols = ['One', 'Two', 'Three', 'Four', 'Five']
      df = pd.DataFrame(np.array(list('ABCDEFGHIJKLMNO'), dtype='object').reshape(3,5), index = list('ABC'), columns=cols)
      df.to_hdf('/tmp/test.h5', 'df')
      df = pd.read_hdf('/tmp/test.h5', 'df')
      df
  Out[1]:
           One     Two     Three  Four    Five
      A    A       B       C      D       E
      B    F       G       H      I       J
      C    K       L       M      N       O
      3 rows × 5 columns
  
  In [2]:
      df.columns = pd.MultiIndex.from_arrays([list('UUULL'), ['One', 'Two', 'Three', 'Four', 'Five']])
      df['L']['Five'] = df['L']['Five'].apply(lambda x: x.lower())
      df
  -c:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
  Try using .loc[row_index,col_indexer] = value instead 
  Out[2]:
           U                      L
           One    Two     Three   Four    Five
      A    A      B       C       D       E
      B    F      G       H       I       J
      C    K      L       M       N       O
      3 rows × 5 columns
  
  In [3]:
      df.columns = ['One', 'Two', 'Three', 'Four', 'Five']
      df    
  Out[3]:
           One    Two     Three   Four    Five
      A    A      B       C       D       E
      B    F      G       H       I       J
      C    K      L       M       N       O
      3 rows × 5 columns
  
  In [4]:
      df['Five'] = df['Five'].apply(lambda x: x.upper())
      df
  Out[4]:
           One    Two     Three   Four    Five
      A    A      B       C       D       E
      B    F      G       H       I       J
      C    K      L       M       N       O
      3 rows × 5 columns
  </code></pre>
  
  <p>As you can see, the function is not applied to the column, I guess because I get this warning:</p>
  
  <pre><code>-c:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
  Try using .loc[row_index,col_indexer] = value instead
  </code></pre>
  
  <p>What is strange is that this error only happens sometimes, and I haven't been able to understand when does it happens and when not. </p>
  
  <p>I managed to apply the function slicing the dataframe with <code>.loc</code> as the warning recommended:</p>
  
  <pre><code>In [5]:
      df.columns = pd.MultiIndex.from_arrays([list('UUULL'), ['One', 'Two', 'Three', 'Four', 'Five']])
      df.loc[:,('L','Five')] = df.loc[:,('L','Five')].apply(lambda x: x.lower())
      df
  
  Out[5]:
           U                      L
           One    Two     Three   Four    Five
      A    A      B       C       D       e
      B    F      G       H       I       j
      C    K      L       M       N       o
      3 rows × 5 columns
  </code></pre>
  
  <p>but I would like to understand why this behavior happens when doing dict-like slicing (e.g. <code>df['L']['Five']</code>) and not when using the <code>.loc</code> slicing. </p>
  
  <p><strong>NOTE</strong>: The DataFrame comes from an HDF file which was not multiindexed is this perhaps the cause of the strange behavior?</p>
  
  <p><strong>EDIT</strong>: I'm using <code>Pandas v.0.13.1</code> and <code>NumPy v.1.8.0</code></p>