Fix groupby().transform() example in cookbook.rst

@jorisvandenbossche : Fixes code in example, and makes final averaging by len(x) stylistically similar to previous adds.
New pull request replacing my earlier one, which contained some other erroneous commits.
closes #8944