Groupby maximum in pandas dataframe python

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

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

 

 

Groupby single column – groupby max pandas python:

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


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

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

Groupby maximum 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'].max().reset_index()

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

Groupby maximum in pandas python 3

 

Groupby multiple columns – groupby max pandas python:


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

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

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

Groupby maximum in pandas python 4

 

Groupby Max 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'].max().reset_index()

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

Groupby maximum in pandas python 5

 

Using aggregate() function:

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

We will compute groupby max 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 maximum 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).max().pivot('State','Product').fillna(0)

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

Groupby maximum in pandas python 6

 

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

    View all posts