Skip to content
Advertisement

Check equality with Python dataframe containing None values

I’m comparing a dataframe to a dict as follows…

JavaScript

Both my dataframe (df) and dict (a) may contain a None value, but I’ve noticed that when 2 None values are compared, they are not deemed to be equal and so I’m printing “not in list” even though both df and a both hold the same value.

Any thoughts on the best way around this issue would be really appreciated. Perhaps I have to convert the None values to a string that will return True when compared?

Advertisement

Answer

The issue here is that np.NAN == np.NANresolves to False. You can use pd.DataFrame.equals, which considers NaNs in the same locations as being equal. In code that reads like

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement