title: pythonic way to aggregate arrays (numpy or not)
body: <p>I would like to make a nice function to aggregate data among an array (it's a numpy record array, but it does not change anything)</p>
  
  <p>you have an array of data that you want to aggregate among one axis: for example an array of <code>dtype=[(name, (np.str_,8), (job, (np.str_,8), (income, np.uint32)]</code> and you want to have the mean income per job</p>
  
  <p>I did this function, and in the example it should be called as <code>aggregate(data,'job','income',mean)</code></p>
  
  <hr>
  
  <pre><code>def aggregate(data, key, value, func):
  
      data_per_key = {}
  
      for k,v in zip(data[key], data[value]):
  
          if k not in data_per_key.keys():
  
              data_per_key[k]=[]
  
          data_per_key[k].append(v)
  
      return [(k,func(data_per_key[k])) for k in data_per_key.keys()]
  </code></pre>
  
  <hr>
  
  <p>the problem is that I find it not very nice I would like to have it in one line: do you have any ideas?</p>
  
  <p>Thanks for your answer Louis</p>
  
  <p>PS: I would like to keep the func in the call so that you can also ask for median, minimum...</p>