You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算started_at与ended_at两列的时间差并填充到duration列

时间差计算解决方案

最终填充结果

started_atended_atduration(分钟)
10/08/2021 17:1510/08/2021 17:227
10/08/2021 17:2310/08/2021 17:3916
21/08/2021 02:3421/08/2021 02:5016
21/08/2021 06:5221/08/2021 07:0816
19/08/2021 11:5519/08/2021 12:049
19/08/2021 12:4119/08/2021 12:476

注意:计算前请确认时间列的解析格式为「日/月/年」,如果原始数据实际为「月/日/年」格式,需对应调整解析参数,避免计算错误。

Python Pandas 实现代码

首先将两列时间转换为正确的 datetime 类型,再计算差值即可,代码如下:

import pandas as pd

# 替换为你实际的数据源读取逻辑
df = pd.read_csv("你的数据文件路径.csv")

# 转换时间格式,适配当前日/月/年 时:分的结构
df['started_at'] = pd.to_datetime(df['started_at'], format='%d/%m/%Y %H:%M')
df['ended_at'] = pd.to_datetime(df['ended_at'], format='%d/%m/%Y %H:%M')

# 计算时间差,取整数分钟结果
df['duration'] = (df['ended_at'] - df['started_at']).dt.total_seconds() // 60

Excel 实现公式

如果数据存储在Excel表格中,假设started_at在A列、ended_at在B列、duration在C列,第二行起为有效数据,在C2单元格输入以下公式,下拉填充即可:

=(B2-A2)*1440

公式中乘以1440是将Excel默认的天单位时间差转换为分钟单位。

内容的提问来源于stack exchange,提问作者Bobo022

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 01:57:03