使用python从日期时间列添加时间间隔列
嗨,我有以下数据帧:使用python从日期时间列添加时间间隔列,python,pandas,datetime,Python,Pandas,Datetime,嗨,我有以下数据帧: assetReference lastPositionUpdate TAG20 2020-07-05 00:01:06 TAG20 2020-07-05 00:03:07 TAG20 2020-07-05 00:05:14 TAG20 2020-07-05 00:07:15 TAG20 2020-07-05 00:09:22 TAG20 2020-07-05 00:11:30 TAG20 2020-07-05 00:13:31 TAG20
assetReference lastPositionUpdate
TAG20 2020-07-05 00:01:06
TAG20 2020-07-05 00:03:07
TAG20 2020-07-05 00:05:14
TAG20 2020-07-05 00:07:15
TAG20 2020-07-05 00:09:22
TAG20 2020-07-05 00:11:30
TAG20 2020-07-05 00:13:31
TAG20 2020-07-05 00:15:39
TAG20 2020-07-05 00:17:47
TAG20 2020-07-05 00:19:54
TAG20 2020-07-05 00:21:55
TAG20 2020-07-05 00:24:03
TAG20 2020-07-05 00:26:11
TAG20 2020-07-05 00:28:18
TAG20 2020-07-05 00:30:27
TAG20 2020-07-05 00:32:34
TAG20 2020-07-05 00:34:43
TAG20 2020-07-05 00:36:50
TAG20 2020-07-05 00:38:58
我想添加一列,将每一行与相应的时间间隔相关联,如下所示:
lastPositionUpdate => Time interval
05/07/2020 00:01:06 => 00:00:00-00:02:00
05/07/2020 00:03:07 => 00:02:00-00:04:00
05/07/2020 00:05:14 => 00:04:00-00:06:00
时间间隔为2分钟,从00:00:00-00:00:02到00:58:00-00:00:00
我是python新手。您能帮我构建一个与您的数据相似的数据框吗
将熊猫作为pd导入
df=pd.DataFrame([
[TAG20',2020-07-05 00:01:06'],
[TAG20',2020-07-05 00:03:07'],
[TAG20',2020-07-05 00:05:14'],
[TAG20',2020-07-05 00:07:15'],
[TAG20',2020-07-05 00:09:22'],
[TAG20',2020-07-05 00:11:30'],
],
列=[''u source.tag.assetReference',''u source.tag.location.lastPositionUpdate']
)
df.loc[:,'\u source.tag.location.lastPositionUpdate']=pd.to\u datetime(df['\u source.tag.location.lastPositionUpdate']))
我们可以添加间隔如下的列:
temp=df.assign(
低=df[''源.标签.位置.最后位置更新'].dt.楼层('2min'),
high=df[''u source.tag.location.lastPositionUpdate'].dt.ceil('2min'))
)
df.loc[:,'interval']=温度应用(λ行:pd.interval(行['low'],行['high']),轴=1)
这可能不是最快的方法,但确实有效:
_source.tag.assetReference _source.tag.location.lastPositionUpdate interval
0 TAG20 2020-07-05 00:01:06 (2020-07-05, 2020-07-05 00:02:00]
1 TAG20 2020-07-05 00:03:07 (2020-07-05 00:02:00, 2020-07-05 00:04:00]
2 TAG20 2020-07-05 00:05:14 (2020-07-05 00:04:00, 2020-07-05 00:06:00]
3 TAG20 2020-07-05 00:07:15 (2020-07-05 00:06:00, 2020-07-05 00:08:00]
4 TAG20 2020-07-05 00:09:22 (2020-07-05 00:08:00, 2020-07-05 00:10:00]
5 TAG20 2020-07-05 00:11:30 (2020-07-05 00:10:00, 2020-07-05 00:12:00]
请提供而不是图像。使用您的行更新帖子