This question was inspired by this other one.
Say that I have the following pandas dataframe:
TYPE YEAR DAY VALUE
0 a 2004 10 NaN
1 b 2005 12 NaN
2 c 2006 180 NaN
3 a 2007 127 NaN
4 b 2008 221 NaN
5 c 2008 17 NaN
and that I have to fill in the VALUE
column based on the following dict of dicts, which has the format {YEAR: {DAY, VALUE}}
:
mydict={2004: {10: 7.1},
2005: {12: 9.19},
2006: {127: 16.04, 180: 12.33},
2007: {55: 21.94, 127: 33.11},
2008: {17: 5.13, 221: 19.17, 300: 10.05}}
The answer given in the post above is to use df.VALUE = df.VALUE.fillna(df.YEAR.map(mydict))
.
How can I change this mapping to make sure it "follows" both the YEAR and DAY columns in my dataframe?
If I apply the snippet above I get of course:
TYPE YEAR DAY VALUE
0 a 2004 10 {10: 7.1}
1 b 2005 12 {12: 9.19}
2 c 2006 180 {127: 16.04, 180: 12.33}
3 a 2007 127 {55: 21.94, 127: 33.11}
4 b 2008 221 {17: 5.13, 221: 19.17, 300: 10.05}
5 c 2008 17 {17: 5.13, 221: 19.17, 300: 10.05}
Instead, I am aiming for the values.