Skip to content

Dask Dataframe Groupby 'First'/'Last' with agg function #3206

Closed

Description

@morganics

As raised on Stackoverflow.

I want to groupby a single column, and then use agg with mean for a couple of columns, but just select first or last for the remaining columns. This is possible in pandas, but isn't currently supported in Dask. Expected usage;

aggs = {'B': 'mean', 'C': 'mean', 'D': 'first', 'E': 'last'}
ddf.groupby(by='A').agg(aggs)

This is one possible solution at the moment (referring to the answer posted on Stackoverflow);

You can use dask.dataframe.DataFrame.drop_duplicates and then join to aggregate DataFrame:

df = pd.DataFrame({'F':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'A':list('aaabbb')})

print (df)
   A  B  C  D  E  F
0  a  4  7  1  5  a
1  a  5  8  3  3  b
2  a  4  9  5  6  c
3  b  5  4  7  9  d
4  b  5  2  1  2  e
5  b  4  3  0  4  f

from dask import dataframe as dd 
ddf = dd.from_pandas(df, npartitions=3)
#print (ddf)


c = ['B','C']
a = ddf.groupby(by='A')[c].mean()
b = ddf.drop(c, axis=1).drop_duplicates(subset=['A'])
df = b.join(a, on='A').compute()
print (df)
   A  D  E  F         B    C
0  a  1  5  a  4.333333  8.0
3  b  7  9  d  4.666667  3.0

But obviously 'first' and 'last' would be a lot easier!

Activity

  1. mrocklin commented on Feb 25, 2018

    @mrocklin
    Member

    If you're interested in contributing this then you might want to take a look at the following bits of code:

    • The implementation of groupby-count:
      @derived_from(pd.core.groupby.GroupBy)
      def count(self, split_every=None, split_out=1):
      return self._aca_agg(token='count', func=M.count,
      aggfunc=M.sum, split_every=split_every,
      split_out=split_out)
    • The docstring of apply_concat_apply:

      dask/dask/dataframe/core.py

      Lines 3103 to 3122 in 41468d6

      @insert_meta_param_description
      def apply_concat_apply(args, chunk=None, aggregate=None, combine=None,
      meta=no_default, token=None, chunk_kwargs=None,
      aggregate_kwargs=None, combine_kwargs=None,
      split_every=None, split_out=None, split_out_setup=None,
      split_out_setup_kwargs=None, **kwargs):
      """Apply a function to blocks, then concat, then apply again
      Parameters
      ----------
      args :
      Positional arguments for the `chunk` function. All `dask.dataframe`
      objects should be partitioned and indexed equivalently.
      chunk : function [block-per-arg] -> block
      Function to operate on each block of data
      aggregate : function concatenated-block -> block
      Function to operate on the concatenated result of chunk
      combine : function concatenated-block -> block, optional
      Function to operate on intermediate concatenated results of chunk
      in a tree-reduction. If not provided, defaults to aggregate.

    I suspect that this is feasible without much in-depth knowledge of Dask dataframe internals.

  2. added
    good first issue
    on Feb 25, 2018
  3. mrocklin commented on May 25, 2018

    @mrocklin
    Member

    I believe that this was resolved by @almaleksia in #3389 . Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dataframegood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions