Groupby count in pandas dataframe python

Groupby count in pandas python can be accomplished by groupby() function. Groupby count 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 count
  • Groupby multiple columns in  groupby count
  • Groupby count using aggregate() function
  • Groupby count using pivot() function.
  • using reset_index() function for groupby multiple columns and single column

Generic Groupby count 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 count in pandas python 1

 

Groupby single column – groupby count pandas python:

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


''' Groupby single column in pandas python'''

df1.groupby(['State'])['Sales'].count()

We will groupby count with single column (State), so the result will be
Groupby count in pandas 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'].count().reset_index()

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

Groupby count in pandas python 3

 

 

Groupby multiple columns – groupby count python :


''' Groupby multiple columns in pandas python'''

df1.groupby(['State','Product'])['Sales'].count()

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

Groupby count in pandas python 4

 

 

Groupby Count 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'].count().reset_index()

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

Groupby count in pandas python 5

 

 

Using aggregate() function:

agg() function takes ‘count’ as input which performs groupby count, 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('count').reset_index()

We will compute groupby count 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 count in pandas python 5

 

 

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).count().pivot('State','Product').fillna(0)

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

Groupby count in pandas python 6

 

p Group by count in pandas dataframe python                                                                                                           n Group by count 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.