Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Replace negative infinity values within a pandas dataframe [duplicate]

As written in the title, I need to replace -inf values within a pandas data frame. I would like to replace them by nan-values. There are multiple columns containing -inf so it should be run over the whole data frame.

I tried df.replace(np.inf, np.nan) which only seems to work with positive infinity. Also tried df.replace('-inf', np.nan).

How do I replace negative infinity values in my dataframe?

like image 876
MaMo Avatar asked Aug 08 '26 18:08

MaMo


1 Answers

Try the below, rather than 'inf' as a string:

df.replace(-np.Inf, np.nan)
like image 92
muzzyq Avatar answered Aug 11 '26 15:08

muzzyq



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!