Learn Data Analysis With Pandas - Aggregates in Pandas
Learn Data Analysis With Pandas - Aggregates in Pandas
Aggregates in Pandas
Pandas’ Groupby
In a pandas DataFrame , aggregate statistic functions can
be applied across multiple rows by using a groupby df = pd.DataFrame([
function. In the example, the code takes all of the ["Amy","Assignment 1",75],
elements that are the same in Name and groups them, ["Amy","Assignment 2",35],
replacing the values in Grade with their mean. Instead
["Bob","Assignment 1",99],
of mean() any aggregate statistics function, like
["Bob","Assignment 2",35]
median() or max() , can be used. Note that to use the
], columns=["Name", "Assignment", "Grade"])
groupby() function, at least two columns must be
supplied.
df.groupby('Name').Grade.mean()