我目前有一个Pandas DataFrame,其中包含许多转义字符中使用的反斜杠。例如,有些字符串的形式为 'Michael's dog'
。
当我使用 pandas.DataFrame.to_csv
将此数据帧保存到 CSV 文件时,我想去掉这些反斜杠,以便 CSV 文件中的条目将被简单地"Michael's dog"
。
有没有一种简单的方法可以做到这一点,无论是利用函数还是方法?我尝试遍历原始数据帧并手动进行更改,但我无法摆脱必须有更有效的方法的感觉。
谢谢。
编辑
很抱歉造成混乱,也许我应该在最初的问题中更具体。
我遇到问题的数据形式如下:
[' ['Mazda', 'it', "Mazda 's", 'its', 'its', "Mazda 's"]',
" ['the 2019 Mazda3', 'the 2019 Mazda3', 'it', 'the 2019 Mazda3', 'The 2019 Mazda3', 'its']",
" ['the car', 'its']",
' ['the Japanese automaker', "the brand 's"]']
如您所见,数据在技术上是一个列表而不是一个字符串,这意味着简单地使用 replace
是行不通的。
不要使用 str.replace,它会简单地替换每个 '\' 字符。
请改用这个:
df.ColumnName.str.decode('unicode_escape')
测试:
>>> data = {'Name':['Tom\\'', 'nick', 'krish', 'jack'], 'Age':[20, 21, 19, 18]}
>>> df = pd.DataFrame(data)
>>> df.Name.str.decode('unicode_escape')
0 Tom'
1 nick
2 krish
3 jack
Name: Name, dtype: object
作者测试:
>>> data
{'Name': [' ['Mazda', 'it', "Mazda 's", 'its', 'its', "Mazda 's"]', " ['the 2019 Mazda3', 'the 2019 Mazda3', 'it', 'the 2019 Mazda3', 'The 2019 Mazda3', 'its']", " ['the car', 'its']", ' ['the Japanese automaker', "the brand 's"]']}
>>> df = pd.DataFrame(data)
>>> df.Name.str.decode('unicode_escape')
0 ['Mazda', 'it', "Mazda 's", 'its', 'its', "Ma...
1 ['the 2019 Mazda3', 'the 2019 Mazda3', 'it', ...
2 ['the car', 'its']
3 ['the Japanese automaker', "the brand 's"]
Name: Name, dtype: object
源:https://stackoverflow.com/a/14820462/6741053