简体   繁体   English

使用Pandas中的fillna()方法替换列中的特定字符串值

[英]Using the fillna() method from Pandas to replace a particular string value in a column

I have a particular column(the column is called 'numbers') which outputs unique values like this: 我有一个特定的列(该列称为“数字”),它输出像这样的唯一值:

df.numbers.unique()

Output: 输出:

([nan, '50', '22', '11', '46', '58', '22', '14', '18', '15', '33', 'XX'], dtype=object)

As seen above there are unidentified characters such as 'XX'. 如上所示,存在未识别的字符,例如“ XX”。 I wish to convert them to 'NaN' values instead. 我希望将它们转换为“ NaN”值。 I tried replacing these with using this code: 我尝试使用以下代码替换这些代码:

 df.numbers.replace('XX',np.NaN)

However, when I print the unique values in the column again. 但是,当我再次在列中打印唯一值时。 The 'XX' is still there. “ XX”仍然存在。 I wish to get rid of the 'XX' and replace them with 'NaN' instead. 我希望摆脱“ XX”,而将其替换为“ NaN”。 I am just curious to know why it is not working. 我只是想知道为什么它不起作用。 Assistance would be appreciated. 协助将不胜感激。 Thanks in advance! 提前致谢!

我想你只是忘了把它分配回来

df.numbers=df.numbers.replace('XX',np.NaN)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中使用 Replace() 或 fillna() 将 NAN 替换为 Pandas 中列的字典值 - Replace NAN with Dictionary Value for a column in Pandas using Replace() or fillna() in Python 如果列以字符串的一部分开头,Pandas 将替换并填充 - Pandas replace and fillna if column begins with part of a string 使用模式将 NaN 替换为最常见的列字符串值时,我无法让 Python 中的 Fillna 工作 - I cannot get Fillna in Python to Work when using Mode to Replace NaNs with Most Frequent Column String Value fillna 通过引用另一列但使用 pandas 复制相同的列值 - fillna by referring another column but copy same column value using pandas 如何使用fillna返回pandas DataFrame中相应列的平均值 - How to return the mean value of the corresponding column in a pandas DataFrame using fillna 使用熊猫根据特定列中的条件替换日期值 - To replace the date value based on the condition in a particular column using pandas 用特定字符串替换列值 - Replace the column value with a particular string 如何将 fillna() 与另一列中的值一起使用并删除 pandas 中的特定列值 - How to use fillna() with values from another column and delete that particular column values in pandas 将列值的字符替换为Pandas中另一列的字符串 - Replace character of column value with string from another column in Pandas 来自具有条件的函数的 Pandas 列 fillna - Pandas column fillna from a function with condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM