title: Fraction of values in (x, y) space
body: <p>I have a data frame  that looks like this, but with several hundred thousand rows:</p>
  
  <pre><code>df
  
      D   x           y
  0   y   5.887672    6.284714
  1   y   9.038657    10.972742
  2   n   2.820448    6.954992
  3   y   5.319575    15.475197
  4   n   1.647302    7.941926
  5   n   5.825357    13.747091
  6   n   5.937630    6.435687
  7   y   7.789661    11.868023
  8   n   2.669362    11.300062
  9   y   1.153347    17.625158
  </code></pre>
  
  <p>I want to know what proportion of values ("D") in each x:y grid space is "n".</p>
  
  <p>I can do it by brute force, by stepping through x and y and calculating the percentage:</p>
  
  <pre><code>zonexy = {}
  for x in np.arange(0,10,2.5):
      dfx = df[(df['x'] &gt;= x) &amp; (df['x'] &lt; x+2.5)]
      zonexy[x] = {}
      for y in np.arange(0,24,6):
          dfy = dfx[(dfx['y'] &gt;= y) &amp; (dfx['y'] &lt; y+6)]
          try:
              pctn = len(dfy[dfy['Descr']=='n'])/len(dfy) * 100.0
          except ZeroDivisionError:
              pctn = 0
          zonexy[x][y] = pctn
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>pd.DataFrame(zonexy)
  
      0.0 2.5 5.0 7.5
  0   0   0   0   0
  6   100 100 50  0
  12  0   0   50  0
  18  0   0   0   0
  </code></pre>
  
  <p>But this, and all the variations on this theme that I've tried, is very slow. It seems like there should be a much more efficient way (probably via numpy), but I'm blanking on it.  </p>