在 Pandas 数据框中用 NaN 替换字符串值 - Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53668421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace a string value with NaN in pandas data frame - Python
提问by stefanodv
Do I have to replace the value? with NaN so you can invoke the .isnull () method. I have found several solutions but some errors are always returned. Suppose:
我必须更换价值吗?使用 NaN 以便您可以调用 .isnull () 方法。我找到了几种解决方案,但总是返回一些错误。认为:
data = pd.DataFrame([[1,?,5],[?,?,4],[?,32.1,1]])
and if I try:
如果我尝试:
pd.data.replace('?', np.nan)
I have:
我有:
0 1 2
0 1.0 NaN 5
1 NaN NaN 4
2 NaN 32.1 1
but data.isnull() returns:
但 data.isnull() 返回:
0 1 2
0 False False False
1 False False False
2 False False False
Why?
为什么?
回答by jezrael
I think you forget assign back:
我想你忘了分配回来:
data = pd.DataFrame([[1,'?',5],['?','?',4],['?',32.1,1]])
data = data.replace('?', np.nan)
#alternative
#data.replace('?', np.nan, inplace=True)
print (data)
0 1 2
0 1.0 NaN 5
1 NaN NaN 4
2 NaN 32.1 1
print (data.isnull())
0 1 2
0 False True False
1 True True False
2 True False False
回答by caverac
?
is a notnull. So you will expect to get a False
under the isnull
test
?
是不是空。所以你会期望得到一个False
under isnull
test
>>> data = pandas.DataFrame([[1,'?',5],['?','?',4],['?',32.1,1]])
>>> data
0 1 2
0 False False False
1 False False False
2 False False False
After you replace ?
with NaN
the test will look much different
?
用NaN
测试替换后看起来会大不相同
>>> data = data.replace('?', np.nan)
>>> data
0 1 2
0 False True False
1 True True False
2 True False False
回答by Som Dubey
I believe when you are doing pd.data.replace('?', np.nan)
this action is not done in place, so you must try -
相信你在做pd.data.replace('?', np.nan)
这个动作的时候是没有做到位的,所以一定要试试——
data = data.replace('?', np.nan)