I have a dataframe in which I have various rows with NaNs that complement each other. I would want to merge these rows so that the new row fills in some or all of the NaN on the old row, based on the values of two specific columns in this row.
To illustrate what I mean, I have the following dataframe
ID1 | ID2 | value1 | value2 | value3 |
---|---|---|---|---|
A | 1 | 1.0 | NaN | NaN |
A | 1 | NaN | 6.2 | NaN |
A | 2 | NaN | NaN | 9.8 |
B | 1 | NaN | NaN | 2.4 |
B | 1 | 3.5 | 3.8 | NaN |
C | 1 | NaN | NaN | 6.3 |
Here, rows 1 and 2 refer to the same IDs, and so do rows 4 and 5. I would like to merge those rows, and 'condense' the dataframe to the following:
ID1 | ID2 | value1 | value2 | value3 |
---|---|---|---|---|
A | 1 | 1.0 | 6.2 | NaN |
A | 2 | NaN | NaN | 9.8 |
B | 1 | 3.5 | 3.8 | 2.4 |
C | 1 | NaN | NaN | 6.3 |
Any logical way to do this?
Thank you for your help