目前正在使用一个有趣的交通智能卡数据集。从a线到B线的电流跳闸(e.B.g。60分钟内的任何行程都需要归入行程。在
当前表格:
CustomerID SegmentID Origin Dest StartTime EndTime Fare Type
0 A001 101 A B 7:30am 7:45am 1.5 Bus
1 A001 102 B C 7:50am 8:30am 3.5 Train
2 A001 103 C B 17:10pm 18:00pm 3.5 Train
3 A001 104 B A 18:10pm 18:30pm 1.5 Bus
4 A002 105 K Y 11:30am 12:30pm 3.0 Train
5 A003 106 P O 10:23am 11:13am 4.0 Ferrie
然后隐藏成这样的东西:
^{pr2}$我是Python和Pandas的新手,不知道如何开始,所以任何指导都将不胜感激。在
这里有一个相当完整的答案。你没有详细说明单程旅行的概念,所以我猜了一下。您可以调整下面的蒙版,以更好地符合您自己的定义。在
首先,我们需要找出一种将行分组的方法。问题1中的“客户ID”中也没有指定时间。请注意,对于三种模式的出行,这实际上意味着第一次和第三次出行的开始时间可能相差超过一个小时,只要第一次+第二次和第二次+第三次分别小于1小时。这看起来是一种很自然的方法,但是对于实际的用例,您必须根据您想要的定义来调整它。有很多方法你可以在这里继续。在
^{pr2}$现在我们可以使用cumsum的掩码来生成tripID:
现在,对于每个列,只需适当地聚合:
注意,持续时间仅包括行程时间,不包括行程之间的时间(例如,如果第二次行程的开始时间晚于第一次行程的结束时间)。在
相关问题 更多 >
编程相关推荐