Closed
Description
Consider this very basic group by operation:
df = pd.DataFrame({'col1': ['a', 'b', 'a', 'c'], 'col2': [1, 3, 2, 5]})
df_grp = df.groupby('col1')['col2']
When aggregating using mean
, min
, ... (everything but count
) the resulting Series has col2
as its name:
df_grp.mean()
col1
a 1.5
b 3.0
c 5.0
Name: col2, dtype: float64
However, if I use count
for aggregation, the name is not set:
df_grp.count()
col1
a 2
b 1
c 1
dtype: int64
Not a big problem for a simple case like that, but I stumbled over that while working with a MultiIndex that needed to be reindexed and led to a KeyError
due to that missing column name.