我的数据框包含
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_numeric
与errors='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/