如何在熊猫fataframe中查找具有相同时间间隔的行

问题描述 投票:0回答:1

我正在DNS记录中定位信标活动,并且我的pandas数据框包含已解析的DNS记录。

结构看起来像这样:

     Received_Time       Sender_IP  Receiver_IP  Content
2019-01-01 23:59:54.999   0.0.0.1     1.1.1.1      ...
2019-01-01 23:59:56.999   0.0.0.1     1.1.1.1      ...
2019-01-01 23:59:57.999   0.0.0.1     1.1.1.1      ...
2019-01-01 23:59:58.999   0.0.0.1     1.1.1.1      ...
2019-01-01 23:59:59.999   0.0.0.1     1.1.1.2      ...

我正在努力实现:

Beacon_Interval(s)  Beacon_Count(including first)  Sender_IP  Receiver_IP
      1.000                      3                  0.0.0.1     1.1.1.1

时间为日期时间类型,我的想法是:

  1. 将“时间间隔添加到上一次查询”。
  2. 然后,很容易统计和查找以相同间隔发送大多数查询的发件人。

我不确定如何执行第一步。我还要发帖看看是否有人有更好的方法来完成任务,在此先感谢您。

python pandas dataframe
1个回答
0
投票

根据我的理解,您想要获得每个“接收时间”的间隔,可以通过此操作来完成:

df['Beacon_Interval(s)'] = df['Received_Time'].diff().dt.seconds

由于我们现在知道'信标间隔',因此我们可以使用以下代码来计算其实例数:

#Note, since you explicitly tell that it should count the first instance, I used +2 instead of +1 in the end.
df['Beacon_Count(including first)'] = \
df.groupby((df['Beacon_Interval(s)'] != df['Beacon_Interval(s)'].shift(1)).cumsum()).cumcount()+2
© www.soinside.com 2019 - 2024. All rights reserved.