title: iterating over index of pandas dataframe
body: <p>Suppose we have a table of (for simplicity)historical stock data, which has columns like</p>
  
  <pre><code>timestamp   ticker open high low close
  </code></pre>
  
  <p>and I want to have a column which has the percentage return for ticker on the date.</p>
  
  <p>Now, the obvious first step is make a multi-index of timestamp and ticker, and then for each ticker one can do something like:</p>
  
  <pre><code>df.loc[idx[:, ['AAPL']], idx['ret']]=df.loc[idx[:, ['AAPL']], idx['close']].pct_chg()
  </code></pre>
  
  <p>What is not clear is how to iterate over the tickers in the most elegant way possible. One can make a list of tickers, and just do a plain python for loop, iterating the above over all of them, but this seems a little clunky. is there a more "pandas" way of doing this?</p>
  
  <p>To answer the comment, for simplicity, let's just have the close, and suppose the data looks like</p>
  
  <pre><code>date ticker close
  Jan 1 A     10
  Jan 2 A     20
  Jan 3 A     30
  Jan 1 B     15
  Jan 2 B     15
  Jan 3 B     7.5
  </code></pre>
  
  <p>The result should be</p>
  
  <pre><code>date  ticker close return
  Jan 1 A       10    NaN
  Jan 2 A       20    1.0
  Jan 3 A       30    0.5
  Jan 1 B       15    NaN
  Jan 2 B       15    0
  Jan 3 B       7.5  -0.5
  </code></pre>