3

I have a Data frame like this,

a  b  c  d  e
1  0  0  4  5
0  23 5  0  0
0  5  8  6  0

Now, i am using a np.log on the entire data frame like this.

df = (np.log(weights_df))

Its all fine and working out. But wherever there is 0, its giving "-inf" as it is supposed to. I want to convert all of these to something else, maybe "0" in place of "-inf". I tried fillna but i do not think its going to work here.

How do i do it?

1
  • 1
    You need to set pd.set_option('use_inf_as_null', True) for inf to be regarded as NaN. Then you can use fillna or dropna for inf values as well. Commented Aug 26, 2016 at 7:31

1 Answer 1

5

-np.inf and np.inf are not considered null or na.

Use replace(-np.inf, 0):

df = (np.log(weights_df)).replace(-np.inf, 0)

df

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.