Reputation: 1
Patient | arrival datetime | depart datetime | Total in department |
---|---|---|---|
1 | 09/04/2024 23:00 | 10/04/2024 11:00 | |
2 | 10/04/2024 07:42 | 10/04/2024 09:57 | |
3 | 10/04/2024 09:15 | 10/04/2024 13:00 | |
4 | 10/04/2024 08:56 | 10/04/2024 10:15 | |
5 | 10/04/2024 05:00 | 10/04/2024 08:30 | |
6 | 10/04/2024 06:15 | 10/04/2024 14:20 | |
7 | 10/04/2024 01:29 | 10/04/2024 10:32 | |
8 | 10/04/2024 08:22 | 10/04/2024 15:15 |
I'm completely lost. I thought I could create a loop if/elif func to go through each row and check if the arrival datetime was <= patient1's arrival datetime and the depart datetime was >= patient 1's arrival datetime then the column would give me a total for how many were in the department at patient1's arrival time and this loop would continue to go through each patient.
Upvotes: 0
Views: 22
Reputation: 18
Here you can use a dataframe to store the data and loop through it to check the number of patients with satisfies given condition this is how you can do this ->
import pandas as pd
data = {
"Patient": [1, 2, 3, 4, 5, 6, 7, 8],
"arrival datetime": ['09/04/2024 23:00', '10/04/2024 07:42', '10/04/2024
09:15', '10/04/2024 08:56', '10/04/2024 05:00', '10/04/2024 06:15',
'10/04/2024 01:29', '10/04/2024 08:22'],
"depart datetime": ['10/04/2024 11:00', '10/04/2024 09:57', '10/04/2024
13:00', '10/04/2024 10:15', '10/04/2024 08:30', '10/04/2024 14:20',
'10/04/2024 10:32', '10/04/2024 15:15']
}
df = pd.DataFrame(data)
df['Total in department'] = 0
df['arrival datetime'] = pd.to_datetime(df['arrival datetime'])
df['depart datetime'] = pd.to_datetime(df['depart datetime'])
for i in range(df.shape[0]):
c = 0
for j in range(df.shape[0]):
if i != j and (df.loc[j, 'arrival datetime'] <= df.loc[i, 'arrival
datetime'] and df.loc[j, 'depart datetime'] >= df.loc[i, 'depart
datetime']):
c += 1
df.loc[i, 'Total in department'] = c
print(df)
You can use other data structures and can solve the problem like wise.
Upvotes: 0