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

Viewed 2528

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

3 Answers

np.where

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

Does the code below work for you? I used .loc[row_indexer,col_indexer] = value to modify the dataframe (link to the documentation)

import pandas as pd
import numpy as np

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

Output:

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

Try instead:

df['A'] = df2['A'].apply(lambda x: x if x > cutoff else None)
Related