【问题标题】:Split spreadsheet into pieces based on the sequences of numbers根据数字序列将电子表格拆分为多个部分
【发布时间】:2020-03-08 20:51:59
【问题描述】:

我在电子表格中有一个数据集,基本上是关于纽约地铁列车每次行程的数据。

╔═══════╦══════╦══════════════╦════════════════╦═════════╦═══════════════╦══════════════════╗
║ trip  ║  id  ║ arrival_time ║ departure_time ║ stop_id ║ stop_sequence ║     Station      ║
╠═══════╬══════╬══════════════╬════════════════╬═════════╬═══════════════╬══════════════════╣
║ GO505 ║ 20_2 ║ 0:06:00      ║ 0:06:00        ║     237 ║             1 ║ Penn Station     ║
║ GO505 ║ 20_2 ║ 0:18:00      ║ 0:18:00        ║     214 ║             2 ║ Woodside         ║
║ GO505 ║ 20_2 ║ 0:23:00      ║ 0:23:00        ║      55 ║             3 ║ Forest Hills     ║
║ GO505 ║ 20_2 ║ 0:25:00      ║ 0:25:00        ║     107 ║             4 ║ Kew Gardens      ║
║ GO505 ║ 20_2 ║ 0:29:00      ║ 0:32:00        ║     102 ║             5 ║ Jamaica          ║
║ GO505 ║ 20_2 ║ 0:47:00      ║ 0:47:00        ║     183 ║             6 ║ Rockville Centre ║
║ GO505 ║ 20_2 ║ 0:50:00      ║ 0:50:00        ║     225 ║             7 ║ Baldwin          ║
║ GO505 ║ 20_2 ║ 0:53:00      ║ 0:53:00        ║      64 ║             8 ║ Freeport         ║
║ GO505 ║ 20_2 ║ 0:56:00      ║ 0:56:00        ║     226 ║             9 ║ Merrick          ║
║ GO505 ║ 20_2 ║ 0:59:00      ║ 0:59:00        ║      16 ║            10 ║ Bellmore         ║
║ GO505 ║ 20_2 ║ 1:02:00      ║ 1:02:00        ║     215 ║            11 ║ Wantagh          ║
║ GO505 ║ 20_2 ║ 1:05:00      ║ 1:05:00        ║     187 ║            12 ║ Seaford          ║
║ GO505 ║ 20_2 ║ 1:07:00      ║ 1:07:00        ║     136 ║            13 ║ Massapequa       ║
║ GO505 ║ 20_2 ║ 1:09:00      ║ 1:09:00        ║     135 ║            14 ║ Massapequa Park  ║
║ GO505 ║ 20_2 ║ 1:12:00      ║ 1:12:00        ║       8 ║            15 ║ Amityville       ║
║ GO505 ║ 20_2 ║ 1:15:00      ║ 1:15:00        ║      38 ║            16 ║ Copiague         ║
║ GO505 ║ 20_2 ║ 1:18:00      ║ 1:18:00        ║     117 ║            17 ║ Lindenhurst      ║
║ GO505 ║ 20_2 ║ 1:23:00      ║ 1:23:00        ║      27 ║            18 ║ Babylon          ║
║ GO505 ║ 20_3 ║ 1:00:00      ║ 1:00:00        ║      27 ║             1 ║ Babylon          ║
║ GO505 ║ 20_3 ║ 1:05:00      ║ 1:05:00        ║     117 ║             2 ║ Lindenhurst      ║
║ GO505 ║ 20_3 ║ 1:08:00      ║ 1:08:00        ║      38 ║             3 ║ Copiague         ║
║ GO505 ║ 20_3 ║ 1:10:00      ║ 1:10:00        ║       8 ║             4 ║ Amityville       ║
║ GO505 ║ 20_3 ║ 1:13:00      ║ 1:13:00        ║     135 ║             5 ║ Massapequa Park  ║
╚═══════╩══════╩══════════════╩════════════════╩═════════╩═══════════════╩══════════════════╝

我需要根据 stop_sequence 中的序列以某种方式将其拆分为多个部分。从 1 到 n(此处为 18)的每个序列表示火车的 1 趟行程。因此,例如,我需要计算每次旅行的每次旅行的时间(即每个最后一个 stop_sequence 的离开时间 - 第一个 stop_sequence 的到达时间)(大约有 5,000 个)。我怎么能做到这一点?我希望我可以用 pandas 将 python 中的列分成几个行程并计算每次行程的时间。但我不知道该怎么做。

我的预期输出是

行程 id ║ 行程时长

GO505 20_2 ║ x:xx:xx

GO505 20_3 ║ x:xx:xx

我是数据科学的新手。请帮忙!

【问题讨论】:

  • 你可以只选择与 id 匹配的行吗?它适用于您的示例
  • @B.Go 大约有 5000 个彼此不相关的 id。所以这不是最好的方法
  • 好的。我的另一个想法是制作一个匹配最多 18 个(巴比伦或宾夕法尼亚站(我猜))的正则表达式,并获取所有匹配项的数组!?
  • @B.Go 哈哈,也不可能,因为有时同一条路线也有18、17或22个车站:(
  • 我猜总有 >1 个站!?但是终点总是相同的 2 个城市吗?

标签: python google-sheets spreadsheet


【解决方案1】:

范围 A:G -> 火车每次行程的数据

I1 单元格:=QUERY({ArrayFormula(A:A&" "&B:B),ArrayFormula(VALUE(C:D))},"select Col1,max(Col3)-min(Col2) where Col1!=' ' group by Col1 label max(Col3)-min(Col2) 'Duration of the trip' format max(Col3)-min(Col2) 'hh:mm:ss'")

函数引用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2022-12-22
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多