我正在尝试在数据框中的所有列中搜索大写的字符串“NONE”,并将其替换为“None”。如果它已经是“无”,我什么都不做。
我尝试使用 lambda 函数,但它不起作用。
ps:在数据None
中不是关键字。它是一个字符串,例如:“无”
df=data.apply(lambda row: 'None' if row.astype(str).str.contains('NONE').any() else row, axis=1)
样本输入
A | B
None | 234
NONE | NONE
565 | 347
预期产出
A | B
None | 234
None | None
565 | 347
尝试这个
df = pd.DataFrame({'A': ['None', 'NONE', '565'],
'B': ['234', 'NONE', '347']})
# replace NONE by None
df = df.replace('NONE', 'None')
print(df)
A B
0 None 234
1 None None
2 565 347
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句