Ok so I have a first dataframe df1:
|timestamp |ip |
|2022-01-06 11:58:53+00:00|1.1.1.5. |
|2022-01-08 03:56:35+00:00|10.10.10.24|
|2022-01-09 22:29:30+00:00|3.3.3.89. |
|2022-03-08 22:37:52+00:00|8.8.8.88. |
And a second dataframe, df2:
|timestamp |other|
|2022-01-07 22:08:59+00:00|other|
|2022-01-07 23:08:59+00:00|other|
|2022-01-09 17:04:09+00:00|other|
|2022-03-05 17:04:09+00:00|other|
And I would like to count how many rows there is in df2 depending on the 2 consecutive timestamps in df1, meaning:
|timestamp |ip |count|
|2022-01-06 11:58:53+00:00|1.1.1.5 |NaN |
|2022-01-08 03:56:35+00:00|10.10.10.24|2 |
|2022-01-09 22:29:30+00:00|3.3.3.89 |1 |
|2022-03-08 22:37:52+00:00|8.8.8.88 |1 |
What I tried is to first create another column in df1 with the previous timestamp with this:
df1 = df1.assign(timestamp_b4=df1.timestamp.shift(1)).fillna({'timestamp_b4': df1.timestamp})
which gives me:
|timestamp |ip |timestamp_b4 |
|2022-01-06 11:58:53+00:00|1.1.1.5 |2022-03-08 22:37:52+00:00|
|2022-01-08 03:56:35+00:00|10.10.10.24|2022-01-06 11:58:53+00:00|
|2022-01-09 22:29:30+00:00|3.3.3.89 |2022-01-08 03:56:35+00:00|
|2022-03-08 22:37:52+00:00|8.8.8.88 |2022-01-09 22:29:30+00:00|
and then do some sort of
s = (df2[df2['timestamp'].between(df1['timestamp'], df1['timestamp_b4'])].size())
But unfortunately it does not work since pandas requires comparing identically-labeled objects.
Is there a good pandas/pythonic way to do that?
Thanks