【发布时间】:2022-08-12 23:47:12
【问题描述】:
我有一个数据框:
id val1 val2
1 \"he bought 0\" \"0\"
2 \"0\" \"0-the student\"
我想将 0 替换为空(\"\"),但仅限于 \"0\" 的情况。所以例如“0-学生”不应该变成“-学生”。我怎么能这样做?期望的结果是:
id val1 val2
1 \"he bought 0\" \"\"
2 \"\" \"0-the student\"
str.replace(\"0\", \"\") 也会改变 \"0-the student\" 和 \"he buy 0\"
-
str.replace(\"^0$\", \"\")应该可以工作 IIRC
标签: python python-3.x string dataframe replace