Skip to content Skip to sidebar Skip to footer

Grouping Rows With Groupby And Converting Date & Time Of Rows Of Start Date-time And End Date- Time Columns

I have a dataset looking like this: I intended to group all the rows having 3 detections at one place ( in column Detection_location) in one hour. I used the following code for g

Solution 1:

Check if this works for you. Inside the aggregate function, you can pass all the values that you want to capture.

df2 = (df.groupby([pd.Grouper(key = 'Detection Date & Time', freq = 'H'),df.Detection_Location],sort=False)['Detection Date & Time']
   .agg(['first','last','size'])).reset_index().rename(columns={"first": "Detection Date & Time - Start", "last": "Detection Date & Time - End", "size": "Tags"})

Post a Comment for "Grouping Rows With Groupby And Converting Date & Time Of Rows Of Start Date-time And End Date- Time Columns"