Grouping rows with Groupby and converting date & time of rows of start date-time and end date- time columns

风格不统一 提交于 2019-12-24 11:18:24

问题


I have a dataset looking like this:

Blast Hole	East Coordinate	North Coordinate	Collar	Theoritical Depth	Tag Detector ID	Date and Time	Detection_Location	Detection Date & Time
64	16745.42	107390.32	2634.45	15.95	385656531	23-08-2018 2:39:34 PM	CV23	2018-09-08 14:18:17
61	16773.48	107382.6	2634.68	16.18	385760755	23-08-2018 2:38:32 PM	CV23	2018-09-08 14:24:19
63	16755.07	107387.68	2634.58	16.08	385262370	23-08-2018 2:39:30 PM	CV23	2018-09-08 14:12:42
105	16764.83	107347.67	2634.74	16.24	385742468	23-08-2018 2:41:29 PM	CV22	2018-09-06 20:02:46
100	16752.74	107360.32	2634.33	15.83	385112050	23-08-2018 2:41:08 PM	CV22	2018-09-06 20:15:42
99	16743.1	107362.96	2634.36	15.86	385087366	23-08-2018 2:41:05 PM	CV22	2018-09-06 20:49:21
35	16747.75	107417.68	2635.9	17.4	385453358	23-08-2018 2:36:09 PM	CV22	2018-09-23 05:47:44
5	16757.27	107452.4	2636	17.5	385662254	23-08-2018 2:35:03 PM	CV22	2018-09-23 05:01:12
19	16770.89	107420.83	2634.81	16.31	385826979	23-08-2018 2:35:50 PM	CV22	2018-09-23 05:52:54

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 grouping the rows falling in one hour per 3 detection:

df2 = df1.groupby([pd.Grouper(key = 'Detection Date & Time', freq = 'H'), 
      df1.Detection_Location]).size().reset_index(name = 'Tags')

This code gave me a result like this:

I would rather like to have result in which each rows have start time when the first detection was there in that hour and when the last detection was seen and thus i would like to have a result like this:

This is the required output:

Detection Date & Time - Start 	Detection Date & Time - End	Detection_Location	Tags
2018-09-06 20:02:46	2018-09-06 20:49:21	CV22	3
2018-09-08 14:12:42	2018-09-08 14:24:19	CV23	3
2018-09-23 05:01:12	2018-09-23 05:47:44	CV22	3

Can anyone suggest what else should i add in my group-by function to get this result.

Thanks


回答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"})


来源:https://stackoverflow.com/questions/57549303/grouping-rows-with-groupby-and-converting-date-time-of-rows-of-start-date-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!