How to group a Pandas DataFrame by a column and compute the mean of each group in Python

Grouping a pandas.DataFrame by a column and computing the mean of each group creates a new DataFrame with one row for each group and for each group calculates an average of the columns which don’t define the groups.

Solution for How to group a Pandas DataFrame by a column and compute the mean of each group in Python : You can use pandas.DataFrame.groupby() and pandas.DataFrame.mean() to compute averages by groups Call DataFrame.groupby(by) with by as a column name or list of column names to group the rows of DataFrame by the values of the column by.

Then, call DataFrame.mean() with the result of the previous step as DataFrame to compute the means of each column in the groups.


how-to-group-a-pandas-dataframe-by-a-column-and-compute-the-mean-of-each-group-in-python