Sample data in .csv format
| No.| IP | Unix_time | # integer unix time
| 1 | 1.1.1.1 | 1563552000 | # equivalent to 12:00:00 AM
| 2 | 1.1.1.1 | 1563552030 | # equivalent to 12:00:30 AM
| 3 | 1.1.1.1 | 1563552100 | # equivalent to 12:01:40 AM
| 4 | 1.1.1.1 | 1563552110 | # equivalent to 12:01:50 AM
| 5 | 1.1.1.1 | 1563552150 | # equivalent to 12:02:30 AM
| 6 | 1.2.3.10 | 1563552120 |
Here's the current working code using pandas groupby( ) and get_group( ) functions:
data = pd.read_csv(some_path, header=0)
root = data.groupby('IP')
for a in root.groups.keys():
t = root.get_group(a)['Unix_time']
print(a + 'has' + t.count() + 'record')
You will see the results below:
1.1.1.1 has 5 record
1.2.3.10 has 1 record
Now, I want some changes.
For the same IP value (e.g., 1.1.1.1), I want to make further sub-groups based on a maximum time interval (e.g., 60 seconds), and count the number of elements in each sub-group. For example, in above sample data:
Start from row 1: row 2 Unix_time value is within 60 seconds, but row 3 is beyond 60 seconds.
Thus, row 1-2 is a group, row 3-4 is a separate group, row 5 is a separate group. In other words, group '1.1.1.1' has 3 sub-groups now.
How to make it?