0

I have NAN values in my dataframe columns and i need to replace it with empty string. I need to perform subtraction on these columns, so when replacing with empty string i am getting error:

unsupported operand types for -:'str' and 'str'.

so while applying style to my dataframe i have added this line, and it is working as well:

df.style.set_na_rep('')

But when i am adding format function to the style, to concat '%' symbol in the columns, the NAN values are reappearing along withe the '%' symbol.

Then syntax is:

df = (df.style.apply(highlight_cells, axis = None).set_na_rep('')).format({'B':'{:}%','C':'{:}%'}).set_table_styles(...)
A B C
ab 8.3% 4.7%
cd 9.0% NAN%
ef NAN% NAN%
gh NAN% 13.9%

can anyone please suggest, I am pretty new in pandas and dataframes. Thanks

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Maleficent
  • 57
  • 8

1 Answers1

0

In the latest version of pandas 1.3.0 set_na_rep() is deprecated and you should use format(na_rep="xx") instead.

If the values are truly missing, i.e. they are pd.nan and pd.isna(val) returns True then the value will be substitututed for the display value of na_rep.

On the other hand if you have previously performed some manipulation and the value is a string representation of 'NAN', then this is not a missing value (it is a string), and the na_rep argument will have no effect.

Actually you can see the relevant issue to this here (https://github.com/pandas-dev/pandas/pull/40060) and the ultimate solution (https://github.com/pandas-dev/pandas/pull/40134)

Attack68
  • 4,437
  • 1
  • 20
  • 40