Groupby mean in pandas dataframe python

Groupby mean in pandas python can be accomplished by groupby() function. Groupby mean of multiple column and single column in pandas is accomplished by multiple ways some among them are groupby() function and aggregate() function.  let’s see how to

  • Groupby single column in pandas – groupby mean
  • Groupby multiple columns in pandas – groupby mean
  • Groupby mean  using aggregate() function
  • Groupby mean using pivot() function.
  • using reset_index() function for groupby multiple columns and single columns

Generic Groupby mean 1

First let’s create a dataframe

import pandas as pd
import numpy as np

data = {'Product':['Box','Bottles','Pen','Markers','Bottles','Pen','Markers','Bottles','Box','Markers','Markers','Pen'],
       'State':['Alaska','California','Texas','North Carolina','California','Texas','Alaska','Texas','North Carolina','Alaska','California','Texas'],
       'Sales':[14,24,31,12,13,7,9,31,18,16,18,14]}

df1=pd.DataFrame(data, columns=['Product','State','Sales'])

df1

df1 will be

Groupby mean in pandas python 11

 

 

Groupby single column – groupby mean pandas python:

groupby() function takes up the column name as argument followed by mean() function as shown below


''' Groupby single column in pandas python'''
df1.groupby(['State'])['Sales'].mean()

We will groupby mean with single column (State), so the result will be

Group by mean in pandas dataframe python 2

using  reset_index()

reset_index() function resets and provides the new index to the grouped by dataframe and makes them a proper dataframe structure


''' Groupby single column in pandas python using reset_index()'''

df1.groupby(['State'])['Sales'].mean().reset_index()

We will groupby mean with “State” column along with the reset_index() will give a proper table structure , so the result will be

Groupby mean in pandas python 13

 

 

Groupby multiple columns – groupby mean pandas python:


''' Groupby multiple columns in pandas python'''
df1.groupby(['State','Product'])['Sales'].mean()

We will groupby mean with State and Product columns, so the result will be

Groupby mean in pandas python 12

 

Groupby Mean of multiple columns in pandas using  reset_index():

reset_index() function resets and provides the new index to the grouped by dataframe and makes them a proper dataframe structure


''' Groupby multiple columns in pandas python using reset_index()'''
df1.groupby(['State','Product'])['Sales'].mean().reset_index()

We will groupby mean with “Product” and “State” columns along with the reset_index() will give a proper table structure , so the result will be

Groupby mean in pandas python 3

 

 

Using aggregate() function:

agg() function takes ‘mean’ as input which performs groupby mean, reset_index() assigns the new index to the grouped by dataframe and makes them a proper dataframe structure


''' Groupby multiple columns in pandas python using agg()'''
df1.groupby(['State','Product'])['Sales'].agg('mean').reset_index()

We will compute groupby mean using agg() function with “Product” and “State” columns along with the reset_index() will give a proper table structure , so the result will be

Groupby mean in pandas python 3

using Pivot() function :

You can use the pivot() functionality to arrange the data in a nice table.


''' Groupby multiple columns in pandas python using pivot()'''

df1.groupby(['State','Product'],as_index = False).mean().pivot('State','Product').fillna(0)


groupby() function along with the pivot function() gives a nice table format as shown below

Groupby mean in pandas python 4

 

Group by mean in pandas dataframe python p                                                                                                      n Group by mean in pandas dataframe python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.