数据框的对数函数

3

我有一个类似这样的数据框:

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

现在,我正在像这样在整个数据框上使用np.log

df = (np.log(weights_df))

一切都运行良好。但是在所有为0的地方,它会显示“-inf”,这是正常现象。我想将所有这些转换为其他内容,比如用“0”代替“-inf”。我尝试过使用fillna,但我不认为它适用于此处。
我该如何做呢?

1
你需要设置 pd.set_option('use_inf_as_null', True) 以使 inf 被视为 NaN。然后你可以使用 fillnadropna 处理 inf 值。 - ayhan
1个回答

5

-np.infnp.inf不被视为null或na。

使用replace(-np.inf, 0)

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

df

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接