title: binning a dataframe in pandas in Python
body: <p>Given the following dataframe in pandas:</p>
  <pre><code>import numpy as np
  df = pandas.DataFrame({&quot;a&quot;: np.random.random(100), &quot;b&quot;: np.random.random(100), &quot;id&quot;: np.arange(100)})
  </code></pre>
  <p>where <code>id</code> is an id for each point consisting of an <code>a</code> and <code>b</code> value, how can I bin <code>a</code> and <code>b</code> into a specified set of bins (so that I can then take the median/average value of <code>a</code> and <code>b</code> in each bin)? <code>df</code> might have <code>NaN</code> values for <code>a</code> or <code>b</code> (or both) for any given row in <code>df</code>.</p>
  <p>Here's a better example using Joe Kington's solution with a more realistic <code>df</code>. The thing I'm unsure about is how to access the <code>df.b</code> elements for each <code>df.a</code> group below:</p>
  <pre><code>a = np.random.random(20)
  df = pandas.DataFrame({&quot;a&quot;: a, &quot;b&quot;: a + 10})
  # bins for df.a
  bins = np.linspace(0, 1, 10)
  # bin df according to a
  groups = df.groupby(np.digitize(df.a,bins))
  # Get the mean of a in each group
  print groups.mean()
  ## But how to get the mean of b for each group of a?
  # ...
  </code></pre>