Skip to content

Instantly share code, notes, and snippets.

@jceresearch
Created December 29, 2014 13:23
Show Gist options
  • Save jceresearch/7b15fd9c7c5489f4b452 to your computer and use it in GitHub Desktop.
Save jceresearch/7b15fd9c7c5489f4b452 to your computer and use it in GitHub Desktop.
Phython - Numpy loop to group an array by certain key, supposed to be the faster way
import numpy as np
data = np.arange(1, 7)
groups = np.array([0,0,1,2,2,1])
unique_groups = np.unique(groups)
sums = []
for group in unique_groups:
sums.append(data[groups == group].sum())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment