Pandas group by chunks not single values
<p>Now I'm kinda confused about grouping stuff using pandas.</p>

<p>I have set of data (over 60k rows) with 3 columns:</p>

<pre><code>2015/12/18 11:12:49 +0300   d1  b1
2015/12/18 11:12:50 +0300   d2  b2
2015/12/18 11:13:08 +0300   d1  b3
2015/12/18 11:13:36 +0300   d2  b4
2015/12/18 11:13:43 +0300   d2  b5
2015/12/18 11:14:21 +0300   d2  c0
2015/12/18 11:14:42 +0300   d2  c1
2015/12/18 11:15:13 +0300   d1  c2
2015/12/18 11:15:19 +0300   d3  c3
</code></pre>

<p>And I need to get count of rows grouped by time periods (let's say 0-4, 4-8, 8-12 etc. by 4 hours) and weekdays and then get a single set for periods within a week.</p>

<p>I can get sum for every hour in a week (time is the name of 1st column):</p>

<pre><code>dind = pd.DatetimeIndex(df.time)
gr = df.groupby([dind.weekday, dind.hour])
gr.size()
</code></pre>

<p>But I can't figure out how to group by chunks and then merge resulting <code>MultiIndex</code> into single index column.</p>

<p>I hope it was clear description of the problem.</p>

<p>The first part of you question, how to group by 4 hour chunks is easy and is addressed in both options below.  <code>df.index.hour // 4</code></p>

<p>The second part was vague as there are several ways to interpret "merge into a single column".  I provided you two alternatives.</p>

<p><strong><em>Option 1</em></strong></p>

<pre><code>gpd = df.groupby([df.index.weekday, df.index.hour // 4]).size()
gpd.index = gpd.index.to_series()
gpd

(4, 2)    9
dtype: int64
</code></pre>

<p><strong><em>Option 2</em></strong></p>

<pre><code>gpd = df.groupby([df.index.weekday, df.index.hour // 4]).size()
gpd.index = ['{}_{}'.format(*i) for i in gpd.index]
gpd

4_2    9
dtype: int64
</code></pre>

python pandas dataframe grouping