Skip to content
Related Articles

Related Articles

Improve Article

Max and Min date in Pandas GroupBy

  • Last Updated : 24 Jan, 2021

Prerequisites: Pandas

Pandas GroupBy is very powerful function. This function is capable of splitting a dataset into various groups for analysis. 


dataframe.groupby([column names])

Along with groupby function we can use agg() function of pandas library. Agg() function aggregates the data that is being used for finding minimum value, maximum value, mean, sum in dataset.


dataframe.agg(dictionary with keys as column name)


  • Import module
  • Create or Load data
  • Use GroupBy function on column that you want
  • Then use agg() function on Date column.
  • Display result

Data frame in Use:



import pandas as pd
import numpy as np
# Creating Dataframe
dataset = {'Group': ['G-2', 'G-3', 'G-3', 'G-2', 'G-2'
                     'G-2', 'G-3', 'G-1', 'G-1', 'G-2'],
           'Date': ['2019-11-04', '2020-05-17', '2020-12-12'
                    '2019-10-15', '2019-01-31', '2019-02-13',
                    '2020-12-25', '2018-06-01', '2018-07-15',
dataset = pd.DataFrame(dataset, columns=['Group', 'Date'])
# using groupby() function on Group column
df = dataset.groupby(['Group'])
# using agg() function on Date column
df2 = df.agg(Minimum_Date=('Date', np.min), Maximum_Date=('Date', np.max))
# Displaying result


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :