I have a DataFrame about connection log with columns Id, targetIP, Time. Every record in this DataFrame is a connection event to one system. Id means this connection, targetIP means the target IP address this time, Time is the connection time. With Values:
| ID | Time | targetIP |
|---|---|---|
| 1 | 1 | 192.163.0.1 |
| 2 | 2 | 192.163.0.2 |
| 3 | 3 | 192.163.0.1 |
| 4 | 5 | 192.163.0.1 |
| 5 | 6 | 192.163.0.2 |
| 6 | 7 | 192.163.0.2 |
| 7 | 8 | 192.163.0.2 |
I want to create a new column under some condition: count of connections to this time's target IP address in the past 2 time units. So the result DataFrame should be:
| ID | Time | targetIP | count |
|---|---|---|---|
| 1 | 1 | 192.163.0.1 | 0 |
| 2 | 2 | 192.163.0.2 | 0 |
| 3 | 3 | 192.163.0.1 | 1 |
| 4 | 5 | 192.163.0.1 | 1 |
| 5 | 6 | 192.163.0.2 | 0 |
| 6 | 7 | 192.163.0.2 | 1 |
| 7 | 8 | 192.163.0.2 | 2 |
For example, ID=7, the targetIP is 192.163.0.2 Connected to system in past 2 time units, which are ID=5 and ID=6, and their targetIP are also 192.163.0.2. So the count about ID=7 is 2.
Looking forward to your help.