问题
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!
回答1:
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
来源:https://stackoverflow.com/questions/53983563/how-to-replace-a-dataframe-column-values-with-nan-based-on-a-condition