Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing -inf values to np.nan in a feature pandas.series [duplicate]

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.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?

Edit:

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

works

BUT:

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

does not work.

like image 258
Javiss Avatar asked Apr 13 '18 09:04

Javiss


People also ask

How to replace NaN values in a pandas Dataframe?

How to replace NaN values in a pandas dataframe ? To replace all NaN values in a dataframe, a solution is to use the function fillna (), illustration Example of how to replace NaN values for a given column ('Gender here')

How to replace NaN values with 0’S in Python?

In the context of our example, here is the complete Python code to replace the NaN values with 0’s: Run the code, and you’ll see that the previous two NaN values became 0’s: You can accomplish the same task, of replacing the NaN values with zeros, by using NumPy:

How to replace values given in pandas series with value?

Pandas Series.replace () function is used to replace values given in to_replace with value. The values of the Series are replaced with other values dynamically. Syntax: Series.replace (to_replace=None, value=None, inplace=False, limit=None, regex=False, method=’pad’) to_replace : How to find the values that will be replaced.

How do I replace Nan with feature scaling?

Feature scaling is an important preprocessing step in machine learning that can help increase accuracy and training speed. Naive Bayes is a simple but powerful machine learning model that is often used for classification tasks. To replace values with NaN, use the DataFrame's replace (~) method.


2 Answers

it should work:

df.replace([np.inf, -np.inf], np.nan,inplace=True)
like image 93
shivsn Avatar answered Nov 15 '22 14:11

shivsn


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
like image 35
jpp Avatar answered Nov 15 '22 13:11

jpp