2
df = pd.DataFrame({'a': list('aabb'), 'b': [1, 2, 3, 4], 'c': [4, 2, 1, 3]})

def agg_func(x, y):
    return sum(x) > sum(y)

I need smth like that

df.groupby('a').agg({('b', 'c'): agg_func})

And expect

enter image description here

1
  • Can you explain the logic and provide the expected output? Commented Jul 20, 2022 at 9:49

1 Answer 1

2

Here I would simple aggregate the columns, and then perform the comparison:

g = df.groupby('a').sum()
out = g['b']>g['c']

With your function (probably less efficient):

out = df.groupby('a').apply(lambda g: agg_func(g['b'], g['c']))

output:

a    False
b     True
dtype: bool
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.