Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a dataframe column values with NaN based on a condition?

For example, The dataframe looks like:

DF=pd.DataFrame([[1,1],[2,120],[3,25],[4,np.NaN],[5,45]],columns=["ID","Age"])

In the Age column, the values below 5 and greater than 100 have to converted to NaN.

Any help is appreciated!

like image 850
Raja Sahe S Avatar asked Mar 04 '23 19:03

Raja Sahe S


1 Answers

Using where and between

df.Age=df.Age.where(df.Age.between(5,100))
df
   ID   Age
0  10   NaN
1  20   NaN
2  30  25.0
like image 158
BENY Avatar answered Apr 09 '23 09:04

BENY