<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _map_by_index(self, function, level=0):
 """ An internal function for maping a function to groups of values based on a multi-index Elements of each record are grouped according to unique value combinations of the multi- index across the given levels of the multi-index. Then the given function is applied to to each of these groups separately. If this function is many-to-one, the result can be recast as a Series indexed by the unique index values used for grouping. """


        if type(level) is int:
            level = [level]

        masks, ind = self._makemasks(index=self.index, level=level)
        nMasks = len(masks)
        newindex = array(ind)
        if len(newindex[0]) == 1:
            newindex = ravel(newindex)
        return self.map(lambda v: asarray([array(function(v[masks[x]])) for x in range(nMasks)]),
                        index=newindex)