Skip to content

GroupBy.count() returns the grouping column as both index and column #5610

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

GroupBy.count() (with the default as_index=True) return the grouping column both as index and as column, while other methods as first and sum keep it only as the index (which is most logical I think). This seems a minor inconsistency to me:

In [41]: data = pd.DataFrame({'name' : ['a', 'a', 'b', 'd'], 'counts' : [3,4,3,2]})
In [42]: data
Out[42]:
   counts name
0       3    a
1       4    a
2       3    b
3       2    d

In [43]: g = data.groupby('name')
In [45]: g.count()
Out[45]:
      counts  name
name
a          2     2
b          1     1
d          1     1

In [46]: g.first()
Out[46]:
      counts
name
a          3
b          3
d          2

In [47]: g.sum()
Out[47]:
      counts
name
a          7
b          3
d          2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions