Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logarithm on Dataframe

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?

like image 640
M PAUL Avatar asked Jan 25 '26 05:01

M PAUL


1 Answers

-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

like image 86
piRSquared Avatar answered Jan 27 '26 01:01

piRSquared



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!