mardi 27 avril 2021

Pandas - How can I replace NaN values from next row if values in different column are the same? [duplicate]

My df looks like bellow

ID Name     SupervisorID    Date
1  John     NaN             2021-02
1  John     123             2021-03
2  Thomas   NaN             2021-02
2  Thomas   234             2021-03
3  Ann      NaN             2021-02
3  Ann      567             2021-03
4  Alice    NaN             2021-03
5  Mark     324             2021-03

I need to fill missing SupervisorID with previus ID from another month (I have only two dates: 2021-02 and 2021-03). So if SupervisorID is NaN and ID, Name columns are the same for ID and Name, it should fill SupervisorID with previus ID, so df should looks like below:

ID Name     SupervisorID    Date
1  John     123             2021-02
1  John     123             2021-03
2  Thomas   234             2021-02
2  Thomas   234             2021-03
3  Ann      564             2021-02
3  Ann      567             2021-03
4  Alice    NaN             2021-03
5  Mark     324             2021-03

I hope, that it is clear.

Best regards and thanks for help!

Aucun commentaire:

Enregistrer un commentaire