I'm a Python beginner.
I'm encountering a problem during my loop to fill an absence matrix.
The absence matrix:
The index represents the date from the beginning of 2020
to today
and the columns represent the USER IDs
.
The dataframe is as follows:
ID_USER NOM PRENOM DATE_first DATE_last
1 X X 30/05/2020 00:00 01/06/2020 23:59
1 X X 01/06/2020 00:00 02/06/2020 23:59
2 X X 01/06/2020 00:00 03/06/2020 23:59
and the result I want:
DATE user1 user2
29/05/2020 0 0
30/05/2020 1 0
01/06/2020 1 1
02/06/2020 1 1
03/06/2020 0 1
The objective is to fill the absence matrix with 1
and 0
.
1
when the ID is absent between DATE_DEBUT_ABSENCE
and DATE_FIN_ABSENCE
.
Exemple :
- if in Dataframe
ID_USER=1
was absent between2020/01/01
and2020/01/05
: - in the absence of matrice in
columns = 1
- index : 2020/01/01 = 1
- 2020/01/02 = 1
- 2020/01/03 = 1
- 2020/01/04 = 1
- 2020/01/05 = 1
Here is the code I started :
for i in agenda.columns:
for j in absence_df.ID_USER:
if i==j and agenda.index[i]==absence_df.iloc[j,4]:
agenda.index[i]==1
else :
print('false')
j=j+1
i= i+1
break
print(agenda)