Search code examples
pythonpython-2.7pandasnan

Pandas: nan->None


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 nans 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.


Solution

  • import pandas as pd
    
    df = pd.DataFrame({"a":[1,None],"b":[None,"foo"]})
    df.where((pd.notnull(df)), None)
    Out[850]: 
          a     b
    0     1  None
    1  None   foo
    df.where((pd.notnull(df)), None).to_dict()
    Out[851]: {'a': {0: 1.0, 1: None}, 'b': {0: None, 1: 'foo'}}