Lostsoul
Lostsoul

Reputation: 26057

Replacing values in dataframe to nan when greater than x?

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?

Upvotes: 2

Views: 3632

Answers (2)

Dan
Dan

Reputation: 45752

Use standard boolean indexing:

imputed_data_x[imputed_data_x < 1] = np.nan

Upvotes: 4

maow
maow

Reputation: 2887

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)

Upvotes: 0

Related Questions