Groupby maximum in pandas dataframe python

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

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

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 max in pandas dataframe python 1

 

Groupby single column – groupby max (maximum) in pandas python:

''' Group by single column in pandas'''
df1.groupby(['State'])['Sales'].max()

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

Group by max in pandas dataframe python 2

 

Groupby multiple columns – groupby max (maximum) in pandas python:

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

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

Group by max in pandas dataframe python 3

 

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