You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Users can currently request the grouping column be part of the computation for various ops by including them as part of a __getitem__. But agg will still exclude these columns.
df = pd.DataFrame({'a': [1, 1, 2], 'b': 3, 'c': 4, 'd': 5})
gb = df.groupby(['a', 'b'])[['a', 'c']]
result = gb.sum()
print(result)
# a c
# a b
# 1 3 2 8
# 2 3 2 4
result2 = gb.agg(lambda x: x.sum())
print(result2)
# c
# a b
# 1 3 8
# 2 3 4
I would expect __getitem__ to only subset columns for groupby rather than being able to add additional (grouping) columns.
The text was updated successfully, but these errors were encountered:
Thanks @ihsansecer! Would you have any interest in adding a test?
I would expect __getitem__ to only subset columns for groupby rather than being able to add additional (grouping) columns.
I've come around to thinking of this as a feature. While groupby typically excludes the grouping column, if there is a use case for including them I suppose it can be good to have.
I've come around to thinking of this as a feature. While groupby typically excludes the grouping column, if there is a use case for including them I suppose it can be good to have.
Yeah I would expect the same and I wasn't aware of this behavior before. Added a test for this
Related to #46944
Users can currently request the grouping column be part of the computation for various ops by including them as part of a
__getitem__
. Butagg
will still exclude these columns.I would expect
__getitem__
to only subset columns for groupby rather than being able to add additional (grouping) columns.The text was updated successfully, but these errors were encountered: