title: GroupBy and aggregate function in Pandas
body: <p>I have a time series dataset as below. I would like to split this into multiple 20s bins, get the min and max timestamps in each bin and add a flag to each bin based on whether there is at least 1 successful result <em>(success: result = 0;  failed: result = 1)</em></p>
  
  <pre><code>data = [{"product": "abc", "test_tstamp": 1530693399, "result": 1},
      {"product": "abc", "test_tstamp": 1530693405, "result": 0},
      {"product": "abc", "test_tstamp": 1530693410, "result": 1},
      {"product": "abc", "test_tstamp": 1530693411, "result": 0},
      {"product": "abc", "test_tstamp": 1530693415, "result": 0},
      {"product": "abc", "test_tstamp": 1530693420, "result": 0},
      {"product": "abc", "test_tstamp": 1530693430, "result": 0},
      {"product": "abc", "test_tstamp": 1530693431, "result": 0}]
  </code></pre>
  
  <p>I'm able to cut the data into 20s intervals using pandas.cut()and get the min and max timestamps for each bin</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  arange = np.arange(1530693398, 1530693440, 20)
  data = [{"product": "abc", "test_tstamp": 1530693399, "result": 1},
      {"product": "abc", "test_tstamp": 1530693405, "result": 0},
      {"product": "abc", "test_tstamp": 1530693410, "result": 1},
      {"product": "abc", "test_tstamp": 1530693411, "result": 0},
      {"product": "abc", "test_tstamp": 1530693415, "result": 0},
      {"product": "abc", "test_tstamp": 1530693420, "result": 1},
      {"product": "abc", "test_tstamp": 1530693430, "result": 1},
      {"product": "abc", "test_tstamp": 1530693431, "result": 1}]
  df = pd.DataFrame(data)
  df['bins'] = pd.cut(df['test_tstamp'], arange)
  output_1 = df.groupby(["bins"]).agg({'result': np.ma.count, 'test_tstamp': {'mindate': np.min, 'maxdate': np.max}})
  
                           test_tstamp               result
                           maxdate     mindate       count
  bins                                                   
  (1530693398, 1530693418]  1530693415  1530693399      5
  (1530693418, 1530693438]  1530693431  1530693420      3
  </code></pre>
  
  <p>and able to find <code>result success</code> and <code>result failed</code> using <code>groupby()</code></p>
  
  <pre><code>output_2 = df.groupby(["bins", "result"]).result.count()
                                       result
   bins                     result        
   (1530693398, 1530693418] 0            3
                            1            2
   (1530693418, 1530693438] 0            3
  </code></pre>
  
  <p>I'm not sure how to combine <code>output_1</code> and <code>output_2</code> so that instead of <code>result count</code> column above, I would like to have <code>result success</code>, <code>result failed</code> and <code>flag</code> columns associated with each <code>bin</code>.</p>
  
  <p>Expected Output:</p>
  
  <pre><code>                             test_tstamp               result    flag
                           maxdate     mindate      success failed  
  bins                                                   
  (1530693398, 1530693418]  1530693415  1530693399  3         2     True
  (1530693418, 1530693438]  1530693431  1530693420  0         3    False
  </code></pre>
  
  <p>Any pointers would help! Thank you!</p>