【问题标题】:Logarithm on Dataframe数据框上的对数
【发布时间】:2017-01-02 17:12:34
【问题描述】:

我有一个这样的数据框,

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,但我认为它不会在这里工作。

我该怎么做?

【问题讨论】:

  • 您需要设置pd.set_option('use_inf_as_null', True) 以使inf 被视为NaN。然后,您也可以使用fillnadropna 作为 inf 值。

标签: python-2.7 pandas numpy dataframe


【解决方案1】:

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

使用replace(-np.inf, 0)

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

df

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2023-03-11
    • 2015-05-22
    • 2018-06-15
    相关资源
    最近更新 更多