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?
Not a real solution but a workaround: you could fillna the columns of a copy of the dataframe. This can be conventiently done for all columns in one call using assign:
import pandas as pd
import numpy as np
df = pd.DataFrame([[np.nan, np.nan],[1,2]], columns=['a','b'])
print(df.assign(a=df.a.fillna('-'), b=df.b.fillna('n/a')).to_string())
# a b
#0 - n/a
#1 1.0 2.0
print(df)
# a b
#0 NaN NaN
#1 1.0 2.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With