如何解决使用Python将字符串中的“ \\”替换为空格
数据框由df [“ Notes”]列组成,其中包含每个观察值的文本。以下是几个观察值的代码段:
'Storm Data (Vol. 11,No. 8) reports associated with Hurricane Camille (August 1969) indicate \\... substantiated amounts up to 31 inches ... falling mainly in Nelson and adjacent counties ....\\" The 14.28 inches at Williamsburg fell in conjunction with Hurricane Floyd."','These were values of 89.90\\ at Montebello Fish Hatchery and 87.33\\" at Roanoke 5.8 SW (CoCoRaHS). These were determined not to be valid statewide precipitation records. A separate report detailing these values is available here."',
我正尝试使用以下代码用空格(“”)替换文本中的“ \”:
df["Notes"] = df["Notes"].replace('\\',"")
代码没有给出任何错误,但我无法将“ \”替换为“”。列的数据类型为“ O”。
解决方法
如果要将字符串函数应用于pandas系列,则需要将.str
应用于列,然后将.replace
应用于列。
df["Notes"] = df["Notes"].str.replace('\\',"")
本质上,.str
方法允许熊猫迭代列中的字符串对象并在每个元素上运行str.replace('\\',"")
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。