Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different na_rep per column

Tags:

python

pandas

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?

like image 957
Michel de Ruiter Avatar asked Apr 22 '26 05:04

Michel de Ruiter


1 Answers

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
like image 134
Stef Avatar answered Apr 23 '26 18:04

Stef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!