本文介绍了在功能pandas.series中将-inf值替换为np.nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将pandas.series功能(数据框的列)中的-inf值替换为np.nan,但我做不到.

I want to replace the -inf values in a pandas.series feature (column of my dataframe) to np.nan, but I could not make it.

我尝试过:

    df[feature] = df[feature].replace(-np.infty, np.nan)
    df[feature] = df[feature].replace(-np.inf, np.nan)
    df[feature] = df[feature].replace('-inf', np.nan)
    df[feature] = df[feature].replace(float('-inf'), np.nan)

但是它不起作用.有什么想法可以替换这些值吗?

But it does not work. Any ideas how to replace these values?

df[feature] =  df[feature].replace(-np.inf, np.nan)

有效

但是:

df =  df.replace(-np.inf, np.nan)

不起作用.

推荐答案

问题可能是您没有分配回原始系列.

The problem may be that you are not assigning back to the original series.

请注意,默认情况下,pd.Series.replace不是 就地操作.下面的代码是一个最小的示例.

Note that pd.Series.replace is not an in-place operation by default. The below code is a minimal example.

df = pd.DataFrame({'feature': [1, 2, -np.inf, 3, 4]})

df['feature'] = df['feature'].replace(-np.inf, np.nan)

print(df)

#    feature
# 0      1.0
# 1      2.0
# 2      NaN
# 3      3.0
# 4      4.0

这篇关于在功能pandas.series中将-inf值替换为np.nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 02:59