Groupby using condition in python pandas

时间:2018-12-03 13:05:29

标签: python-3.x pandas-groupby

I have a Table with details of Name,Priority,Date_Time

Name       Priority                      Date_Time   
ABC         P1                   01/02/2017  06:30  
BC          P2                   02/04/2017   14:50  
XX          P1                   04/06/2017  02:00   
ANM         P2                   03/05/2017  22:15  
MAC         P1                   04/05/2017 16:40

I need to write code to count name with groupby of priority and Time with condition of specific time. for example 6AM to 12PM, 12PM to 22PM,etc. How to retrieve total name count based on priority with specific time?

1 个答案:

答案 0 :(得分:0)

Pandas groupby supports grouping by sets of columns. What you want could be achieved by first mapping datetimes into groups, and then grouping by that new mapping compounded with Priority. For example

def group_datetimes(dt):
    if 0 <= dt.hour < 6:
        return 1
    elif 6 <= dt.hour < 12:
        return 2
    # etc.

df['datetime_group'] = df['Date_Time'].apply(group_datetimes)
agg = df.groupby(['Priority', 'datetime_group']).count()

According to the Pandas Groupby User Guide you can also group directly with the function, so the above block can be shortened to

def group_datetimes(dt):
    if 0 <= dt.hour < 6:
        return 1
    elif 6 <= dt.hour < 12:
        return 2
    # etc.

agg = df.groupby(['Priority', group_datetimes]).count()