Pandas Group by 2 列使用另一列查找增量

我有一个 Pandas 数据框,它有 4909144 行,time作为索引source_name,dest_address, 和tvalue它与time索引相同。我已经整理由DF source_name,dest_address以及tvalue使用以下,使它们按时间分组,然后依次是:


df = df.sort_values(by=['sourcehostname','destinationaddress','tvalue'])

这给了我:


                        source_name  dest_address   tvalue                 

time                

2019-02-06 15:00:54.000 source_1     72.21.215.90   2019-02-06 15:00:54.000 

2019-02-06 15:01:00.000 source_1     72.21.215.90   2019-02-06 15:01:00.000 

2019-02-06 15:30:51.000 source_1     72.21.215.90   2019-02-06 15:30:51.000 

2019-02-06 15:30:51.000 source_1     72.21.215.90   2019-02-06 15:30:51.000 

2019-02-06 15:00:54.000 source_1     131.107.0.89   2019-02-06 15:00:54.000 

2019-02-06 15:01:14.000 source_1     131.107.0.89   2019-02-06 15:01:14.000 

2019-02-06 15:03:02.000 source_2     69.63.191.1    2019-02-06 15:03:02.000 

2019-02-06 15:08:02.000 source_2     69.63.191.1    2019-02-06 15:08:02.000 

我想要时间之间的差异,所以我然后使用:


#Create delta

df['delta'] = (df['tvalue']-df['tvalue'].shift()).fillna(0)

这给了我:


                        source_name  dest_address   tvalue                 delta

time                

2019-02-06 15:00:54.000 source_1     72.21.215.90   2019-02-06 15:00:54.000 00:00:00

2019-02-06 15:01:00.000 source_1     72.21.215.90   2019-02-06 15:01:00.000 00:00:06

2019-02-06 15:30:51.000 source_1     72.21.215.90   2019-02-06 15:30:51.000 00:29:51

2019-02-06 15:30:51.000 source_1     72.21.215.90   2019-02-06 15:30:51.000 00:00:00

但我想按source_nameand分组dest_address并获得差异,tvalue这样我就不会在第一个条目后遇到 delta喜欢-1 days +23:30:00或delta喜欢00:01:48的source_2时候应该是00:00:00。


我在尝试:


df.groupby(['sourcehostname','destinationaddress'])['tvalue'].diff().fillna(0)

但这需要很长时间,并且可能无法为我提供我正在寻找的结果。



HUX布斯
浏览 140回答 1
1回答

森栏

import datetime as dtsource_changed = df['sourcehostname'] != df['sourcehostname'].shift()dest_changed = df['destinationaddress'] != df['destinationaddress'].shift()change_occurred = (source_changed | dest_changed)time_diff = df['tvalue'].diff()now = dt.datetime.utcnow()zero_delta = now - nowdf['time_diff'] = time_diffdf['change_occurred'] = change_occurred# Then do a function# If df['change_occurred'] is True -> set the value of df['delta'] to zero_delta  # Else set df['delta'] to the value at df['time_dff']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python