简体   繁体   English

如何根据条件用NaN替换数据框列值?

[英]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 wherebetween

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.

相关问题 如何根据第二个 Dataframe 值的条件替换 Dataframe 列值 - How to Replace Dataframe Column Values Based on Condition of Second Dataframe Values 如何根据具有一系列值的条件替换 pd 数据框列中的值? - How to Replace values in a pd dataframe column based on a condition with a range of values? 如何根据Fillna中的where条件替换数据框中的零和nan值 - How to replace a zero and nan values in dataframe based in where condition in fillna or replace 如何根据同一 dataframe 中另一列的值替换 Dataframe 中列中的 NaN 值 - How to replace NaN value in column in Dataframe based on values from another column in same dataframe 根据条件替换数据框列中的值 - Replace values in a dataframe column based on condition 根据条件替换列中的值,然后返回数据框 - Replace values in column based on condition, then return dataframe 如何根据另一列的条件替换一列的NaN值? - How to replace NaN values from a column based on a condition from another column? 根据 pandas dataframe 中的相邻列将 NaN 值替换为特定文本 - Replace NaN values with specific text based on adjacent column in pandas dataframe pandas Dataframe基于键列,将NaN值替换为以前的值 - pandas Dataframe Replace NaN values with with previous value based on a key column 根据条件将 Pandas DataFrame 中的一行替换为“NaN” - Replace a row in Pandas DataFrame with 'NaN' based on condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM