I have a pandas DataFrame where each cell contains a python dict.
>>> data = {'Q':{'X':{2:2010}, 'Y':{2:2011, 3:2009}},'R':{'X':{1:2013}}}
>>> frame = DataFrame(data)
>>> frame
Q R
X {2: 2010} {1: 2013}
Y {2: 2011, 3: 2009} NaN
I'd like to replace the NaN with an empty dict, to get this result:
Q R
X {2: 2010} {1: 2013}
Y {2: 2011, 3: 2009} {}
However, because the fillna
function interprets empty dict not as a scalar value but as a mapping of column --> value, it does NOTHING if I simply do this (i.e. it doesn't work):
>>> frame.fillna(inplace=True, value={})
Q R
X {2: 2010} {1: 2013}
Y {2: 2011, 3: 2009} NaN
Is there any way to use fillna
to accomplish what I want?
Do I have to iterate through the entire DataFrame or construct a silly dict with all my columns mapped to empty dict?
所有评论(0)