I created a dataframe with pandas:
looks like that:
| HostName | Date |
|---|---|
| B | 2021-01-01 12:30 |
| A | 2021-01-01 12:45 |
| C | 2021-01-01 12:46 |
| A | 2021-02-01 12:42 |
| B | 2021-02-01 12:43 |
| A | 2021-02-01 12:45 |
| B | 2021-02-25 12:46 |
| C | 2021-03-01 12:41 |
| A | 2021-03-01 12:42 |
| A | 2021-03-01 12:43 |
| C | 2021-03-01 12:45 |
For every day, it should count how many different HostName there is form the beginning of the day (example: 2021-01-01 00:00) to the specific row
Example: for example lets take the 2021-01-01
| HostName | Date |
|---|---|
| B | 2021-01-01 12:30 |
| A | 2021-01-01 12:45 |
| C | 2021-01-01 12:46 |
there is tree rows:
- the first result would be 1 - because its was the first row in the day.(B)
- the second result would be 2 - because form the beginning of the day till this line there is two different Hostname (B,A)
- the third result would be 3 - because form the beginning of the day till this line there is tree different Hostname ( B,A,C)
the end result should look like this:
| HostName | Date | Result |
|---|---|---|
| B | 2021-01-01 12:30 | 1 |
| A | 2021-01-01 12:45 | 2 |
| C | 2021-01-01 12:46 | 3 |
| A | 2021-02-01 12:42 | 1 |
| B | 2021-02-01 12:43 | 2 |
| A | 2021-02-01 12:45 | 2 |
| B | 2021-02-25 12:46 | 1 |
| C | 2021-03-01 12:41 | 1 |
| A | 2021-03-01 12:42 | 2 |
| A | 2021-03-01 12:43 | 2 |
| C | 2021-03-01 12:45 | 2 |
what it try do to but failed:
df.groupby(['HostName','Date')['HostName'].cumcount() + 1
or
def f(x):
one = x['HostName'].to_numpy()
twe = x['Date'].to_numpy()
both = x[['HostName','Date']].shift(1).to_numpy()
x['Host_1D_CumCount_Conn'] = [np.sum((one == a) & (twe == b)) for a, b in both]
return x
df.groupby('HostName').apply(f)