1
df = pd.DataFrame(
    [
        ["a", 1],
        ["a", 11],
        ["c", 12],
        ["c", 15]
    ],
    columns=["col1","col2"]
)

grouped = df.groupby(["col1"])["col2"].sum().reset_index(name="ccc")
print(grouped)

(As example, taken from here)

This is bit unclear for me, how resetting index is related to aliasing/naming new aggregated column, but even more I don't get why this not raises: reset_index() got an unexpected keyword argument 'name' error, because as doc says there is no "name" argument for reset_index().

Any help is appreciated

Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236

1 Answers1

1

Reason is ouput of aggregation one column (specified after groupby) is Series, so is called Series.reset_index with name parameter:

name, optional

The name to use for the column containing the original Series values. Uses self.name by default. This argument is ignored when drop is True.

not DataFrame.reset_index without name parameter.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thank you, "Reason is ouput of aggregation one column is Series", it sems not all aggregate function returning series? I mean `count` for example, throws error: `grouped = df.groupby(["col1"]).count().reset_index(name="ccc")` – Oto Shavadze Jul 14 '21 at 07:38
  • @OtoShavadze - It is expected, because `df.groupby(["col1"]).count()` return `DataFrame` – jezrael Jul 14 '21 at 07:39
  • @OtoShavadze - Working well `grouped = df.groupby(["col1"])['col1'].count().reset_index(name="ccc")` – jezrael Jul 14 '21 at 07:40