I have the following df:
date family ID value
2019 abc 10 0.5
15 0.3
xyz 22 0.4
40 1.1
2020 xyz 22 1.9
46 0.3
I would like to forward fill value only if the family does not exist in a date. In the example above, the ID and value would forward fill to 2020 for abc, but not for xyz.
I have tried df.unstack(['family','ID']).fillna(method = 'ffill'), however this will incorrectly forward fill ID 40 for family xyz in the above example.
Desired result is :
date family ID value
2019 abc 10 0.5
15 0.3
xyz 22 0.4
40 1.1
2020 abc 10 0.5
15 0.3
xyz 22 1.9
46 0.3
df.unstack(['family','ID']).ffill().stack(['family','ID'])seems to work as expected.