Search code examples
pythonpython-polars

GroupBy, column selection and mean in python-polars


I'm trying to translate this pandas statement:

  df.groupby(['X', 'Y'])['Z'].mean()

to polars, but i'm stuck at this point:

  df.group_by(['X', 'Y'], maintain_order=True).select(pl.col('Z').mean())

where i get this error:

  AttributeError: 'GroupBy' object has no attribute 'select'

Any help would be very appreciated!


Solution

  • You were close

    df.group_by(['X', 'Y'], maintain_order=True).agg(pl.col('Z').mean())