Skip to content Skip to sidebar Skip to footer

Replacing -inf Values To Np.nan In A Feature Pandas.series

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. I have tried: df[feature] = df[feature].replace(-np.

Solution 1:

it should work:

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

Solution 2:

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

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

Post a Comment for "Replacing -inf Values To Np.nan In A Feature Pandas.series"