Skip to content

columns selection after groupby reset group_keys to True #9959

Closed
@ruoyu0088

Description

@ruoyu0088
df = pd.DataFrame({"g":[1, 2, 1, 2, 1], "a":range(5), "b":range(1, 6), "c":range(2, 7)})
g = df.groupby("g", group_keys=False)
print g.group_keys  #False
print g[["a", "b", "c"]].group_keys  #True

So the apply() results are different for these two case:

print g.apply(lambda x:x[:2])

ouput:

   a  b  c  g
0  0  1  2  1
2  2  3  4  1
1  1  2  3  2
3  3  4  5  2

but

print g[["a", "b", "c"]].apply(lambda x:x[:2])

output:

     a  b  c
g           
1 0  0  1  2
  2  2  3  4
2 1  1  2  3
  3  3  4  5

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugGroupby

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions