Replacing values in dataframe to nan when greater than x?

Viewed 1685

I'm trying to replace values in a column to NaN. I normally use

imputed_data_x = imputed_data_x.replace(0, np.nan)

But my problem is that my values are not exactly 0, some are 0.01111,etc. How can I replace all values in a data frame that is less than 1?

I tried imputed_data_x = imputed_data_x.replace(>1, np.nan)

But it didn't work. I'm curious to see if I can use replace to do this or do I need a different command for conditions?

2 Answers

DataFrame.replace is just for replacing fixed values. In your case you want to replace if the value is "close" to 0 which you can express as a predicate function. The API command to replace a value where the predicate returns false (keep the value where it is true) is

imputed_data_x = imputed_data_x.where(lambda x: x >= 1, np.nan)
Related