Hierarchical grouping in pandas
<p>How can you manipulate dataframes at multiple levels using groupby? I want to be able to do something like </p>

<blockquote>
  <p>data.groupby('col1').groupby(['col2', 'col3']).apply(foo).apply(bar)</p>
</blockquote>

<p>Sample data:</p>

<pre><code>    user_id year    day hour    events
0   1928375096  2015    196 0   6
1   734605009   2016    32  21  1
2   3333305045  2016    29  5   3
3   698115442   2016    30  7   11
4   685465592   2016    26  12  3
5   485945404   2016    24  10  4
6   73202588    2016    25  3   1
7   4380205067  2016    25  8   1
8   408502597   2016    32  9   1
9   584885164   2016    32  10  3
</code></pre>

<p>Let's say col1 = user_id, col2 = 'year', col3 = 'day', so we get a number of rows for each user for each day (up to 24). We want to first run foo on events; e.g., <code>foo(x) = (x-x.mean())/x.std()</code>, then we want to reduce each user's time series to a scalar with <code>bar</code>. The resulting data frame should contain one row per user.</p>