Pandas DataFrames with NaNs equality comparison

前端 未结 5 1919
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 07:26

In the context of unit testing some functions, I\'m trying to establish the equality of 2 DataFrames using python pandas:

ipdb> expect
                            


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 07:46

    df.fillna(0) == df2.fillna(0)
    

    You can use fillna(). Documenation here.

    from pandas import DataFrame
    
    # create a dataframe with NaNs
    df = DataFrame([{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}])
    df2 = df
    
    # comparison fails!
    print df == df2
    
    # all is well 
    print df.fillna(0) == df2.fillna(0)
    

提交回复
热议问题