groupby in python without pandas

Solutions on MaxInterview for groupby in python without pandas by the best coders in the world

showing results for - "groupby in python without pandas"
Emmanuel
08 Jul 2019
1class Groupby:
2    def __init__(self, keys):
3        _, self.keys_as_int = np.unique(keys, return_inverse = True)
4        self.n_keys = max(self.keys_as_int) + 1
5        self.set_indices()
6        
7    def set_indices(self):
8        self.indices = [[] for i in range(self.n_keys)]
9        for i, k in enumerate(self.keys_as_int):
10            self.indices[k].append(i)
11        self.indices = [np.array(elt) for elt in self.indices]
12        
13    def apply(self, function, vector, broadcast):
14        if broadcast:
15            result = np.zeros(len(vector))
16            for idx in self.indices:
17                result[idx] = function(vector[idx])
18        else:
19            result = np.zeros(self.n_keys)
20            for k, idx in enumerate(self.indices):
21                result[self.keys_as_int[k]] = function(vector[idx])
22
23        return result