Values are not converted to np.Nan

Asked

Viewed 126 times

0

I have the function:

def zero_to_null(x):
    if x == 0:
        x = np.nan
    return x

Uso series.apply:

Series.apply(zero_to_null)

However, the values 0 are not converted to np.Nan:

Series.value_counts()

0        35
2         4
1         3
13        2
151       2
4         2
...

Does anyone know what’s going on?

1 answer

0

The call Series.apply Pandas, like most calls, doesn’t change the value "inplace" - will it return a new series (or dataframe - it’s hard to say, does it look like you called your variable "Series"? This is the class name of Pandas and creates confusion)

Anyway, what’s missing is reassigning the name to the result of the apply

Series = Series.apply(zero_to_null)
  • 1

    Got it. It worked. I didn’t call the variable Series, I just put a generic name to represent what I was doing. Thank you so much for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.