pandas.DataFrame.to_dict
converts nan
to nan
and null
to None
. As explained in Python comparison ignoring nan this is sometimes suboptimal.
Is there a way to convert all nan
s to None
? (either in pandas
or later on in Python)
E.g.,
>>> df = pd.DataFrame({"a":[1,None],"b":[None,"foo"]})
>>> df
a b
0 1.0 None
1 NaN foo
>>> df.to_dict()
{'a': {0: 1.0, 1: nan}, 'b': {0: None, 1: 'foo'}}
I want
{'a': {0: 1.0, 1: None}, 'b': {0: None, 1: 'foo'}}
instead.