title: Applying a function for a multi-index dataframe
body: <p>I have a multi-index dataframe that is indexed by date and stock. Here is an example:</p>
  
  <pre><code>                     Column1
  Date        Ticker
  2008-08-01  AAPL     value1
              MSFT     value2
              IBM      value3
              etc.
  2008-08-02  AAPL     value4
              MSFT     value5
              IBM      value6
              etc.
  </code></pre>
  
  <p>What I am trying to do is apply a function to this column for each date, and then replace that column. For example, say for each date, starting with 2008-08-01, I want to the average of Column1 and take the difference between each ticker's value and this average. And then Column1 will be replaced by these values. I'd end up having:</p>
  
  <pre><code>                     Column1
  Date        Ticker
  2008-08-01  AAPL     avg(Column1, 8/1/08) - value1
              MSFT     avg(Column1, 8/1/08) - value2
              IBM      avg(Column1, 8/1/08) - value3
              etc.
  2008-08-02  AAPL     avg(Column1, 8/2/08) - value4
              MSFT     avg(Column1, 8/2/08) - value5
              IBM      avg(Column1, 8/2/08) - value6
              etc.
  </code></pre>
  
  <p>I can achieve this by doing:</p>
  
  <pre><code>df = df.copy()
  col1_adjusted = [func(df.loc[df.index.get_level_values('Date') == date]['Column1']) for date in dates]
  col1_adjusted = [item for sublist in col1_adjusted for item in sublist]
  df.Column1= col1_adjusted 
  </code></pre>
  
  <p>But that takes a long time because I have a lot of dates. Is there a better way to do this?</p>