How do I format NaN values differently in different columns? Say with .to_string. I would like some columns to use '-', other columns '?' or just ''. But na_rep only allows a single string.
Until recently, one could use a formatter function:
df.to_string(float_format=lambda x: "-" if pd.isna(x) else f"{x:0.2f}") Using formatters similarly, one could format NaNs in different columns differently.
However, this behavior was considered a bug and has been fixed recently (pandas 1.2.0).
So now that NaNs don't go through formatters, how do I apply different formats to NaNs in different columns?
NANthat inheritsNaNbut then you overload the__repr__and/or__str__and replace allNaNwithNAN