groupby()

noun

say it: “GROOP-by” /ˈɡrupˌbaɪ/

A pandas method that splits a table's rows into groups by the values in one column, so each group can be added up or averaged.

What does groupby() do in pandas?

groupby() is the pandas method that sorts a table's rows into groups by a column's value, so each group can be totaled, counted or averaged on its own.

import pandas as pd
sales = pd.DataFrame({"store": ["Main", "Elm", "Main"], "cups": [300, 410, 320]})
print(sales.groupby("store")["cups"].sum())
# store
# Elm     410
# Main    620

In a sentence

  • “She used groupby() to average the test scores by class period.”
  • “groupby() on the store column gave each store's total cups.”

Level: Journeyman · More Python terms at this level

Updated

In other subjects