First set up a mask for the groups in which you expand the groups in another dimension
mask=(groups==unique(groups).reshape(-1,1)) mask array([[ True, True, True, False, False, False], [False, False, False, True, False, False], [False, False, False, False, True, True]], dtype=bool)
now multiply with val
mask*val array([[1, 2, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 5, 6]])
now you can already do prod along axis 1, except for those zeros that are easy to fix:
prod(where(mask*val,mask*val,1),axis=1) array([ 6, 4, 30])