title: Taking single value from a grouped data frame in Pandas
body: <p>I am a new Python convert (from Matlab). I am using the pandas <code>groupby</code> function, and I am getting tripped up by a seemingly easy problem. I have written a custom function that I <code>apply</code> to the grouped df that returns 4 different values. Three of the values are working great, but the other value is giving me an error. Here is the original <code>df</code>:</p>
  
  <pre><code>Index,SN,Date,City,State,ID,County,Age,A,B,C
  0,32,9/1/16,X,AL,360,BB County,29.0,negative,positive,positive
  1,32,9/1/16,X,AL,360,BB County,1.0,negative,negative,negative
  2,32,9/1/16,X,AL,360,BB County,10.0,negative,negative,negative
  3,32,9/1/16,X,AL,360,BB County,11.0,negative,negative,negative
  4,35,9/1/16,X,AR,718,LL County,67.0,negative,negative,negative
  5,38,9/1/16,X,AR,728-13,JJ County,3.0,negative,negative,negative
  6,38,9/1/16,X,AR,728-13,JJ County,8.0,negative,negative,negative
  7,30,9/1/16,X,AR,728-13,JJ County,8.0,negative,negative,negative
  8,30,9/1/16,X,AR,728-13,JJ County,14.0,negative,negative,negative
  9,30,9/1/16,X,AR,728-13,JJ County,5.0,negative,negative,negative
  ...
  </code></pre>
  
  <p>This is the function that transforms the data. Basically, it counts the number of 'positive' values and the total number of observations in the group. I also want it to return the ID value, and this is where the problem is:</p>
  
  <pre><code>def _ct_id_pos(grp):
      return grp['ID'][0], grp[grp.A == 'positive'].shape[0], grp[grp.B == 'positive'].shape[0], grp.shape[0]
  </code></pre>
  
  <p>I  <code>apply</code> the <code>_ct_id_pos</code> function to the data grouped by <code>Date</code> and <code>SN</code>:</p>
  
  <pre><code>FullMx_prime = FullMx.groupby(['Date', 'SN']).apply(_ct_id_pos).reset_index()
  </code></pre>
  
  <p>So, the method should return something like this: </p>
  
  <pre><code>     Date  SN   ID       0
  0  9/1/16  32  360  (360,2,1,4)
  1  9/1/16  35  718  (718,0,0,1)
  2  9/2/16  38  728  (728,1,0,2)
  3  9/3/16  30  728  (728,2,0,3)
  </code></pre>
  
  <p>But, I keep getting the following error:</p>
  
  <pre><code>...
  KeyError: 0
  </code></pre>
  
  <p>Obviously, it does not like this part of the function: <code>grp['ID'][0]</code> . I just want to take the first value of <code>grp['ID']</code> because--if there are multiple values--they should all be the same (i.e., I could take the last, it does not matter). I have tried other ways to index, but to no avail.</p>