2 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
打開regex_
out = df.replace(" ", "\ ",regex=True)
col1 col2
0 hi\ there 3
1 what\ are\ you\ doing 4
2 cool 5

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
使用.str.replace或.replace(..., regex=True)來替換子字符串。也是\轉(zhuǎn)義字符,所以最好使用原始字符串或其他轉(zhuǎn)義字符:
df['col1'] = df['col1'].str.replace(' ', r'\ ')
輸出:
0 hi\ there
1 what\ are\ you\ doing
2 cool
Name: col1, dtype: object
輸出:
col1 col2
0 hi\ there 3
1 what\ are\ you\ doing 4
2 cool 5
添加回答
舉報(bào)