Max and Min date in Pandas GroupBy

Max and Min date in Pandas GroupBy

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



Last Updated on October 19, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs