title: Vectorized groupby with NumPy
body: <p>Pandas has a widely-used <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="noreferrer">groupby</a> facility to split up a DataFrame based on a corresponding mapping, from which you can apply a calculation on each subgroup and recombine the results.</p>
  
  <p>Can this be done flexibly in NumPy without a native Python for-loop?  With a Python loop, this would look like:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  
  &gt;&gt;&gt; X = np.arange(10).reshape(5, 2)
  &gt;&gt;&gt; groups = np.array([0, 0, 0, 1, 1])
  
  # Split up elements (rows) of `X` based on their element wise group
  &gt;&gt;&gt; np.array([X[groups==i].sum() for i in np.unique(groups)])
  array([15, 30])
  </code></pre>
  
  <p>Above 15 is the sum of the first three rows of <code>X</code>, and 30 is the sum of the remaining two.</p>
  
  <p>By "flexibly,” I just mean that we aren't focusing on one particular computation such as sum, count, maximum, etc, but rather passing any computation to the grouped arrays.</p>
  
  <p>If not, is there a faster approach than the above?</p>