pandas groupby without aggregation (how to)

102 views
Skip to first unread message

Marian K

unread,
Feb 28, 2023, 10:44:29 AM2/28/23
to PyData

hello,

I’m trying to groupby a dataframe without aggregation. I’ve found some solutions, but I’m getting some weird outputs.

import pandas as pd

df = pd.DataFrame( {'key': ['A', 'B', 'A', 'B'], 'value': [2,2,1,1]})

print(df)

print(df.groupby('key').nth[:]) # only this one works

print(df.groupby('key').head()) # doesn't work

print(df.groupby('key').filter(lambda x:True)) # doesn't work

print(df.groupby('key').apply(lambda x:x)) # doesn't work

In cases where it doesn’t work, it does not throw error, it just prints the original df dataframe. Any suggestions why?

p.s. the dataframe example and one of the solutions is picked from StackOverflow, but the other guy also commented that he was unable to reproduce the suggested…

image

image905×125 11.7 KB

Source: https://stackoverflow.com/questions/36069373/pandas-groupby-dataframe-store-as-dataframe-without-aggregating/36069578#36069578

Marian K

unread,
Feb 28, 2023, 10:59:32 AM2/28/23
to PyData
just a follow up,
I have simplified the dataframe, and posted it as an issue here:

I'm attaching here the screenshot of the gist here.
image.png
Reply all
Reply to author
Forward
0 new messages