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 · Fixed by #24748
Closed

GroupBy.apply calling function twice #10519

cancan101 opened this issue Jul 6, 2015 · 6 comments · Fixed by #24748

Comments

@cancan101
Copy link
Contributor

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

jreback commented Jul 6, 2015

dupe of #2936 and its mentioned here in the docs

@cancan101
Copy link
Contributor Author

@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
Copy link
Contributor

jreback 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
Copy link
Contributor Author

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

@jreback
Copy link
Contributor

jreback commented Jul 6, 2015

then loop your self

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

@cancan101
Copy link
Contributor Author

Okay.
Should be:

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

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

Successfully merging a pull request may close this issue.

2 participants