Search code examples
pythonpandasdataframenannonetype

Replace values in a dataframe column that are below a certain threshold with NaN


Let's say I have the following sample dataframe:

df = pd.DataFrame({'A': [4, 0.2, 3, 0.5], 'B': ['red', 'white', 'blue', 'green']})

     A      B
0  4.0    red
1  0.2  white
2  3.0   blue
3  0.5  green

I am trying to replace entries in a column that are below a certain threshold with NaN to look like the following:

     A      B
0  4.0    red
1  NaN  white
2  3.0   blue
3  NaN  green

Here is my attempt:

cutoff = 2
df['A'] = df['A'].apply(lambda x: [y if y > cutoff else None for y in x])

And the error I receive:

TypeError: 'float' object is not iterable

Where have I gone wrong? I assume it has to do with the None type


Solution

  • np.where

    df['A'] = np.where(df['A']<=cutoff , np.nan, df['A'])