Skip to content
Advertisement

pandas DataFrame style, highlight nan’s

Say with this DataFrame

JavaScript

How can I check which element is nan inside df.applymap? (ie, not using df.isnull)

The problem comes from where I want to use the pandas html styling. We have the built-in nan highlighting

JavaScript

but it changes the background colour, instead I want “nan” to be displayed in red.

So I need to do it myself with applymap

JavaScript

But how can I check if a value is nan, when it can also be datetime/string? np.isnan will fail on strings. np.isreal(x) and np.isnan(x) also fails on datetime.

Advertisement

Answer

You can use pd.isnull(), which deals with a wider range of types for missing values check:

JavaScript

enter image description here

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