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