gpt4 book ai didi

python - Pandas 数据帧 : Convert Integer time hh:mm

转载 作者:行者123 更新时间:2023-11-28 22:24:20 27 4
gpt4 key购买 nike

我的DataFrame包含

 dep_time   sched_dep_time
0 517.0 515
1 533.0 529
2 0.0 0.0
3 544.0 545
4 1.0 101

我希望它在 5:17 , 5:15 作为出发时间和预定出发时间,NaN 填充 0 也 1.0 表示在 1:00 出发尝试以下

flight_data['dep_time']=flight_data['dep_time'].astype(str).apply(lambda x: 
pd.to_timedelta(x[:-2] + x[-2:]+':00'))

值错误:需要 hh:mm:ss 格式,已收到:21 天 13:00:00.000000000:00我什至不确定应该如何转换像 0,1,2.. 这样的 dep_time。谢谢并感谢您的帮助

最佳答案

  • pd.to_numericerrors='coerce'
  • fillna(0)
  • pd.to_timedelta

d1 = pd.to_numeric(df.stack(), errors='coerce').fillna(0)
(pd.to_timedelta(d1 // 100, unit='T') +
pd.to_timedelta(d1 % 100, unit='S')).unstack()

dep_time sched_dep_time
0 00:05:17 00:05:15
1 00:05:33 00:05:29
2 00:00:00 00:00:00
3 00:05:44 00:05:45
4 00:00:01 00:01:01

关于python - Pandas 数据帧 : Convert Integer time hh:mm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46431335/

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