gpt4 book ai didi

python - 如何将两个日期时间列合并为一? Pandas python

转载 作者:行者123 更新时间:2023-12-01 23:45:50 25 4
gpt4 key购买 nike

我想要两个变换两列开始和结束:

                         begin                          end
0 NaN 2019-10-21 07:48:28.272688
1 NaN 2019-10-21 07:48:28.449916
2 2019-10-21 07:48:26.740378 NaN
3 2019-10-21 07:48:26.923764 NaN
4 NaN 2019-10-21 07:48:41.689466
5 2019-10-21 07:48:37.306045 NaN
6 NaN 2019-10-21 07:58:00.774449
7 2019-10-21 07:57:59.223986 NaN
8 NaN 2019-10-21 08:32:37.004455
9 2019-10-21 08:32:35.755252 NaN

将一列时间戳与另一列标记结合起来:

                     Timestamp                          Flag
0 2019-10-21 07:48:28.272688 end
1 2019-10-21 07:48:28.449916 end
2 2019-10-21 07:48:26.740378 begin
3 2019-10-21 07:48:26.923764 begin
4 2019-10-21 07:48:41.689466 end
5 2019-10-21 07:48:37.306045 begin
6 2019-10-21 07:58:00.774449 end
7 2019-10-21 07:57:59.223986 begin
8 2019-10-21 08:32:37.004455 end
9 2019-10-21 08:32:35.755252 begin

但目前我找不到将两列开始和结束合并为一列的解决方案。

感谢您的宝贵时间!

最佳答案

使用stackreset_index :

(df.stack()
.rename_axis([None, 'Flag'])
.reset_index(level=1, name='Timestamp'))

[输出]

    Flag                    Timestamp
0 end 2019-10-21 07:48:28.272688
1 end 2019-10-21 07:48:28.449916
2 begin 2019-10-21 07:48:26.740378
3 begin 2019-10-21 07:48:26.923764
4 end 2019-10-21 07:48:41.689466
5 begin 2019-10-21 07:48:37.306045
6 end 2019-10-21 07:58:00.774449
7 begin 2019-10-21 07:57:59.223986
8 end 2019-10-21 08:32:37.004455
9 begin 2019-10-21 08:32:35.755252

关于python - 如何将两个日期时间列合并为一? Pandas python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60603500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com