Search code examples
pythonpandasdategroup

How to group / count date data by the same check in date and check out date in python?


I'm trying to group the date data based on the same check in and check out date, for example:

import pandas as pd

guess = [1, 2, 3, 4, 5]
check_in = ['17/07/2021', '17/07/2021','17/07/2021', '18/07/2021','18/07/2021']
check_out = ['20/07/2021', '20/07/2021','22/07/2021', '24/07/2021', '24/07/2021' ]
data = pd.DataFrame({'guess':guess, 'check in date': check_in, 'check out date': check_out})

>>> data

simulation data

I want to group those data by the same check in and the same check out date, like this (the expected output):

grouped data

is there any function or library in python that can help me group this type of data?? thanku


Solution

  • First use groupby() using your desired columns followed by size():

    data.groupby(['check in date', 'check out date']).size()
    

    Output:

    check in date  check out date
    17/07/2021     20/07/2021        2
                   22/07/2021        1
    18/07/2021     24/07/2021        2