numpy
имеет isin
, что аналогично pandas.isin
Pandas groupby
, выберите данные (row
) и примените функцию к group
.
def groupby(data, bin_data, grouper, agg):
'''
data: numpy array
bin_data: bin's data
grouper: callable, which give returns a list of array values.
agg: callable, to be applied on group
'''
res = {}
for key,arr in grouper(data, bin_data):
res.update({key, agg(arr)})
return res
# Find the indices where `bins == b` and then use them to select the `arry` values
bin_grouper = lambda arry, bvalue: [(b, arry[np.isin(bvalue, b)]) for b in bvalue]
# Compute result
gdata = groupby(weights.numpy(), bins.numpy(), bin_grouper, np.sum)