简体   繁体   English

计算python Pandas中行之间的数据时间差异

[英]Calculate datatime difference between rows in python Pandas

I'm trying to calculate the datetime difference between rows for each unique machine_id here.我正在尝试计算每个唯一 machine_id 的行之间的日期时间差异。 I have already grouped the Dataframes and have tried我已经对数据框进行了分组并尝试过

newdf = newdf.copy()
newdf['diffs'] = float('nan')
newdf = newdf.copy()
for index in newdf.index.levels[0]:
    newdf.diffs[index] = newdf.event_datetime[index].diff

the dataset looks like数据集看起来像

https://i.stack.imgur.com/eg93C.png https://i.stack.imgur.com/eg93C.png

Have you tried diff after groupby operation?您在 groupby 操作后尝试过diff吗? Something like:就像是:

newdf.groupby('machine_id').event_date.diff()

I tried to create multi index data frame, it should work fine using diff() function.我尝试创建多索引数据框,使用diff()函数应该可以正常工作。

using newdf.groupby('machine_id').event_date.diff() suggested by ATL should work fine.使用 ATL 建议的newdf.groupby('machine_id').event_date.diff()应该可以正常工作。 o

# hierarchical indices and columns
index = pd.MultiIndex.from_product([[598, 615, 721], [43, 43, 45]],
                                   names=['machine_id', 'prod_category_id'])

# mock some data
data = ['2017-03-20 12:00:00','2017-03-29 01:00:00','2017-04-29 01:00:00',
        '2017-03-30 02:00:00', '2017-04-29 02:00:00','2017-05-29 12:00:00',
        '2017-10-30 02:00:00', '2017-11-29 02:00:00', '2017-11-29 04:00:00']

# create the DataFrame
newdf = pd.DataFrame(data, index=index)
newdf.columns = ['event_date']

newdf['event_date'] = pd.to_datetime(newdf['event_date'])
newdf.groupby(level=0)['event_date'].diff()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM