这是数据框的子集:
id sentence ADR WD
1 really bad symptoms 1
2 did not help NAN 1
我需要替换"nan" values and "blank cells" with "0"
输出应如下所示。
id sentence ADR WD
1 really bad symptoms 1 0
2 did not help 0 1
这是我的代码:
df_M = df_M.replace(r'\s+', np.nan, regex=True).fillna(0)
但问题是代码也用“0”代替了句子。这是代码的输出:
id sentence ADR WD
1 0 1 0
2 0 0 1
我该如何解决?
答案 0 :(得分:1)
分两步,假设"空白" ='':
foo = pd.DataFrame([['asdf',1,''],['fdsa',np.nan,1]])
foo.replace(np.nan,0)
foo.replace('',0)
如果您想要花哨,可以一步到位
foo.fillna(0).replace('',0)