Groupby minimum in pandas dataframe python

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

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

 

 

Groupby single column – groupby min pandas python:

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

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

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

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

Groupby minimum 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'].min().reset_index()

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

Groupby minimum in pandas python 3

 

Groupby multiple columns – groupby min pandas python:

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

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

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

Groupby minimum in pandas python 4

 

 

Groupby Min 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'].min().reset_index()

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

Groupby minimum in pandas python 5

 

Using aggregate() function:

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

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

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

Groupby minimum in pandas python 6

 

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