python - 如何将具有 "end"和 "start"行的事件数据帧转换为按事件数据帧重新组合的数据帧?

标签 python pandas datetime data-science data-analysis

我有一个按时间顺序排序的事件数据集。我使用 Pandas 数据框。这是数据框的样子:

Time                         Event   Location    ID
2020-05-22 21:22:04.784622   start   UK          50
2020-05-22 21:43:07.060629   end     UK          50
2020-05-25 23:22:04.784622   start   UK          50
2020-05-25 23:43:07.060629   end     UK          50
2020-05-25 23:44:15.000566   start   US          30
2020-05-25 23:48:23.416348   start   Italy       70
2020-05-26 00:48:06.820164   end     US          30
2020-05-26 01:33:42.454450   end     Italy       70
2020-05-27 20:48:23.416348   start   Italy       30
2020-05-27 00:33:42.454450   end     Italy       30
etc

这就是我想要的:
Start_Time                   End_Time                    Location    ID
2020-05-22 21:22:04.784622   2020-05-22 21:43:07.060629  UK          50
2020-05-25 23:22:04.784622   2020-05-25 23:43:07.060629  UK          50
2020-05-25 23:44:15.000566   2020-05-26 00:48:06.820164  US          30
2020-05-25 23:48:23.416348   2020-05-26 01:33:42.45445   Italy       70
2020-05-27 20:48:23.416348   2020-05-27 00:33:42.454450  Italy       30
etc


我尝试制作单独的数据帧(一个用于开始,一个用于结束)并将它们合并到 Location 和 ID 上,但显然它不起作用。我也看过类似的问题,但无法从那里弄清楚。
有人会知道我如何做到这一点吗?

编辑:此外,数据框中会有多个具有相同位置或 ID 的事件。编辑示例中的数据以更准确地反射(reflect)我的数据集

最佳答案

一种方法是在最后三列上设置索引,和 unstack之后的事件列。

df = pd.read_clipboard(sep='\s{2,}', engine='python', parse_dates=['Time'])

res = (df
       #appending Event,Location and ID with current index
       #prevents duplicate values when unstacking
       .set_index(['Event','Location','ID'], append=True)
       #get Event index as column
       .unstack('Event')
       #topmost column level redundant ... remove
       .droplevel(0,axis=1)
       #fill upwards on the end to align the dates to 
       #the appropriate positions
       .assign(end = lambda x: x['end'].bfill())
       .dropna()
       .add_suffix("_time")
       .reset_index()
       .drop("level_0", axis=1)
       .reindex(['start_time','end_time','Location','ID'], axis=1)
       .rename_axis(None,axis=1)
      )

res



          start_time                      end_time      Location    ID
0   2020-05-22 21:22:04.784622  2020-05-22 21:43:07.060629  UK      50
1   2020-05-25 23:22:04.784622  2020-05-25 23:43:07.060629  UK      50
2   2020-05-25 23:44:15.000566  2020-05-26 00:48:06.820164  US      30
3   2020-05-25 23:48:23.416348  2020-05-26 00:48:06.820164  Italy   70
4   2020-05-27 20:48:23.416348  2020-05-27 00:33:42.454450  Italy   30

关于python - 如何将具有 "end"和 "start"行的事件数据帧转换为按事件数据帧重新组合的数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019602/

相关文章:

python - 我们可以在 Python 中添加 'OR' 运算符以及 if 和 else 吗?

python - 将多个元组添加到单个字典键而不合并元组?

python - GraphLab Create 安装错误 - Python 3.5.2

python - 当数据点为 float 据类型时,如何将列与最近的数据点合并?

PHP/MySQL 随机无法解析正确的日期时间

python - Python和openCV数组IndexError:列表分配索引超出范围

python - 如何用 pandas 编写 multiIndex-columns excel

python - Pandas 适用,但仅适用于满足条件的行

javascript - 为什么我的 Date 克隆在 Travis CI 测试中落后了 1 小时

python - Pandas 日期时间格式 VS 谷歌表格日期格式