Groupby count in pandas dataframe python

Groupby count in pandas python can be accomplished by groupby() function. let’s see how to

  • Groupby single column in pandas – groupby count
  • Groupby multiple columns in pandas – groupby count

First let’s create a dataframe


import pandas as pd
import numpy as np

data = {'Name':['James','Paul','Richards','Marico','Samantha','Ravi','Raghu','Richards','George','Ema','Samantha','Catherine'],
       '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=['Name','State','Sales'])

print(df1)

df1 will be

Group by count in pandas dataframe python 1

 

Groupby single column – groupby count pandas python:


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

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

Group by count in pandas dataframe python 2

 

Groupby multiple columns – groupby count pandas python:


''' Groupby multiple columns '''
df1.groupby(['State','Name'])['Sales'].count()

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

Group by count in pandas dataframe python 3

 

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