Open In App
Related Articles

Max and Min date in Pandas GroupBy

Improve Article
Improve
Save Article
Save
Like Article
Like

Prerequisites: Pandas

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

Syntax:

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.

Syntax:

dataframe.agg(dictionary with keys as column name)

Approach:

  • 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:

Program:

Python3




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',
                    '2019-09-14']}
  
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
display(df2)


Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 Jan, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials