Search code examples
pandaspandas-styles

how do you remove text from Pandas styled object while keeping the style in place


I have a styled object that some of the cells have nan values. I need to remove the nan values while I keep the style in place.

enter image description here

if I do this:

new_df.data.replace(np.nan, '', regex=True)

this removes the nan values as well as the style.

Is there a way to keep the style while removing nan values from the Pandas styled object.


Solution

  • Edit: I noticed your issue is with the styles.

    You could use this oneliner:

    styler_obj.data.where(~styler_obj.data.isna(), '', inplace=True)
    

    If you don't care about the styles you can simply use DataFrame.replace like this:

    df.replace('NaN', '', inplace=True)
    

    Or get a little bit fancy using types from Numpy like this:

    import numpy as np
    df.replace(np.nan, '', inplace=True)