时间计算、均值、中位数、众数

Time calculations, mean , median, mode

( Name Gun_time Net_time Pace
John 28:48:00 28:47:00 4:38:00
George 29:11:00 29:10:00 4:42:00
Mike 29:38:00 29:37:00 4:46:00
Sarah 29:46:00 29:46:00 4:48:00
Roy 30:31:00 30:30:00 4:55:00

Q1。如何添加另一列说明 Gun_time 和 Net_time 之间的区别? Q2。我将如何计算 Gun_time 和 Net_time 的平均值。请帮忙!

我已尝试执行以下操作,但它不起作用

df['Difference'] = df['Gun_time'] - df['Net_time']

对于平均值,我尝试了 df['Gun_time'].mean

还是不行,求助!

Q.3 如果我们有 28:48(分钟和秒)格式的时间而不是 28:48:00 函数给出值错误怎么办。

ValueError:预期 hh:mm:ss 格式

将您的列转换为 dtype timedelta,例如喜欢

for col in ("Gun_time", "Net_time", "Pace"):
    df[col] = pd.to_timedelta(df[col])

现在你可以像这样计算

df['Gun_time'].mean()
# Timedelta('1 days 05:34:48')  

df['Difference'] = df['Gun_time'] - df['Net_time']

#df['Difference']
# 0   0 days 00:01:00
# 1   0 days 00:01:00
# 2   0 days 00:01:00
# 3   0 days 00:00:00
# 4   0 days 00:01:00
# Name: Difference, dtype: timedelta64[ns]

如果你需要更好的字符串输出,你可以使用

def timedeltaToString(td):
    hours, remainder = divmod(td.total_seconds(), 3600)
    minutes, seconds = divmod(remainder, 60)
    return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}"


df['diffString'] = df['Difference'].apply(timedeltaToString)

# df['diffString']
# 0    00:01:00
# 1    00:01:00
# 2    00:01:00
# 3    00:00:00
# 4    00:01:00
#Name: diffString, dtype: object

另见 Format timedelta to string