[英]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. 在“年龄”列中,小于5且大于100的值必须转换为NaN。
Any help is appreciated! 任何帮助表示赞赏!
Using where
and between
where
和between
df.Age=df.Age.where(df.Age.between(5,100))
df
ID Age
0 10 NaN
1 20 NaN
2 30 25.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.