groupby (overcomplicated slow solution)