python 所有列中截止时间和开始时间的代码时间差
python code time difference with cut-off time and start time in all columns
我想在 pandas 列中找到截止时间和结束时间列表的差异,并在 df
中追加新时间
from datetime import datetime, date
out = datetime.combine(date.today(), datetime.strptime('00:15', '%H:%M').time())
print(out)
# output : datetime.datetime(2020,2,8,0,15)
reception time
2022-02-07 18:01:58
2022-02-07 05:05:05
2022-02-07 08:07:34
2022-02-07 09:05:33
find difference of all time and append in new column in df
如果您的 reception time
是数据框的一列,请使用:
df['diff'] = out - pd.to_datetime(df['reception time'])
print(df)
# Output
reception time diff
0 2022-02-07 18:01:58 0 days 06:13:02
1 2022-02-07 05:05:05 0 days 19:09:55
2 2022-02-07 08:07:34 0 days 16:07:26
3 2022-02-07 09:05:33 0 days 15:09:27
设置:
data = {'reception time': ['2022-02-07 18:01:58', '2022-02-07 05:05:05',
'2022-02-07 08:07:34', '2022-02-07 09:05:33']}
df = pd.DataFrame(data)
print(df)
# Output
reception time
0 2022-02-07 18:01:58
1 2022-02-07 05:05:05
2 2022-02-07 08:07:34
3 2022-02-07 09:05:33
我想在 pandas 列中找到截止时间和结束时间列表的差异,并在 df
中追加新时间from datetime import datetime, date
out = datetime.combine(date.today(), datetime.strptime('00:15', '%H:%M').time())
print(out)
# output : datetime.datetime(2020,2,8,0,15)
reception time
2022-02-07 18:01:58
2022-02-07 05:05:05
2022-02-07 08:07:34
2022-02-07 09:05:33
find difference of all time and append in new column in df
如果您的 reception time
是数据框的一列,请使用:
df['diff'] = out - pd.to_datetime(df['reception time'])
print(df)
# Output
reception time diff
0 2022-02-07 18:01:58 0 days 06:13:02
1 2022-02-07 05:05:05 0 days 19:09:55
2 2022-02-07 08:07:34 0 days 16:07:26
3 2022-02-07 09:05:33 0 days 15:09:27
设置:
data = {'reception time': ['2022-02-07 18:01:58', '2022-02-07 05:05:05',
'2022-02-07 08:07:34', '2022-02-07 09:05:33']}
df = pd.DataFrame(data)
print(df)
# Output
reception time
0 2022-02-07 18:01:58
1 2022-02-07 05:05:05
2 2022-02-07 08:07:34
3 2022-02-07 09:05:33