计算 Excel 列或 .hdf 文件中第一行和最后一行之间的时间差
Calculate time difference between first and last row in Excel column or .hdf file
我在 Excel 和 .hdf 数据框中都有“日期时间”列。如何计算第一行和最后一行之间的时间差(以小时、分钟或秒为单位)?这是我的数据的样子;请记住,我的数据有几千行。因此我无法编写代码并手动添加这些日期:
(P.S。我对 python 很陌生,这是我的第一个代码)
请参阅下面的 table 以了解它的外观:
如您所见,我的日期和时间在一栏中:
Datetime Header: Machine_started
2021-02-02 14:33:09 Data 1
2021-02-02 14:33:09 Data 1
2021-02-02 14:33:11 Data 1
2021-02-02 14:41:36 Data 1
我创建了一个演示数据框:
import pandas as pd
import numpy as np
data = {"Datetime": ['2021-02-02 14:33:09', '2021-02-02 14:33:09', '2021-02-02 14:33:11', '2021-02-02 14:41:36'],
"Header": ['Data', 'Data','Data','Data'],
"1_2_eBeam_started": [1,1,1,1]}
df = pd.DataFrame(data)
# creating dataframe
df['Datetime'].dtype
# dtype is object
# convert it to datetime
df['Datetime']=pd.to_datetime(df['Datetime'])
df['Datetime'].iloc[0] # this is first row
df['Datetime'].iloc[-1] # this is last row
# difference in seconds:
(df['Datetime'].iloc[-1] - df['Datetime'].iloc[0])/np.timedelta64(1,'s')
#output 507.0
# You can also get the difference in minutes, hours, etc. by rplacing 's' by 'm' or 'h' in np.timedelta64(1,'s')
我在 Excel 和 .hdf 数据框中都有“日期时间”列。如何计算第一行和最后一行之间的时间差(以小时、分钟或秒为单位)?这是我的数据的样子;请记住,我的数据有几千行。因此我无法编写代码并手动添加这些日期: (P.S。我对 python 很陌生,这是我的第一个代码)
请参阅下面的 table 以了解它的外观: 如您所见,我的日期和时间在一栏中:
Datetime Header: Machine_started
2021-02-02 14:33:09 Data 1
2021-02-02 14:33:09 Data 1
2021-02-02 14:33:11 Data 1
2021-02-02 14:41:36 Data 1
我创建了一个演示数据框:
import pandas as pd
import numpy as np
data = {"Datetime": ['2021-02-02 14:33:09', '2021-02-02 14:33:09', '2021-02-02 14:33:11', '2021-02-02 14:41:36'],
"Header": ['Data', 'Data','Data','Data'],
"1_2_eBeam_started": [1,1,1,1]}
df = pd.DataFrame(data)
# creating dataframe
df['Datetime'].dtype
# dtype is object
# convert it to datetime
df['Datetime']=pd.to_datetime(df['Datetime'])
df['Datetime'].iloc[0] # this is first row
df['Datetime'].iloc[-1] # this is last row
# difference in seconds:
(df['Datetime'].iloc[-1] - df['Datetime'].iloc[0])/np.timedelta64(1,'s')
#output 507.0
# You can also get the difference in minutes, hours, etc. by rplacing 's' by 'm' or 'h' in np.timedelta64(1,'s')