Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GroupBy.apply calling function twice #10519

Closed
cancan101 opened this issue Jul 6, 2015 · 6 comments

Comments

@cancan101
Copy link
Contributor

commented Jul 6, 2015

def foo(x):
    print "hello"
_ = pd.DataFrame({'a':[1]}).groupby(["a"]).apply(foo)
hello
hello
@jreback

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2015

dupe of #2936 and its mentioned here in the docs

@cancan101

This comment has been minimized.

Copy link
Contributor Author

commented Jul 6, 2015

@jreback I am confused in reading those docs: what is the single run version of apply that can be used when I want to return a new dataframe for each grouping that is that concatenated to form one resulting dataframe?

e.g what is the simples way to apply this:

def foo(x):
    print 'xxx'
    return x

and have it called once per group and end up with the same dataset that went in coming out?

@jreback

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2015

df.groupby(...).apply(foo)

not sure why it matters if its called twice. This is only to determine there is mutation or not.

@cancan101

This comment has been minimized.

Copy link
Contributor Author

commented Jul 6, 2015

foo is a very expensive operation that I would prefer is run just once.

@jreback

This comment has been minimized.

Copy link
Contributor

commented Jul 6, 2015

then loop your self

``pd.concat([ (g, foo(grp)) for g, grp in df.groupby(....) ])`

@cancan101

This comment has been minimized.

Copy link
Contributor Author

commented Jul 6, 2015

Okay.
Should be:

pd.concat([ foo(grp) for _, grp in df.groupby(...) ])

@jreback jreback added this to the 0.25.0 milestone Jan 26, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.