Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

pandas - Replace multiple characters of a string in Python

I have a column called Review Text with details like:

Review text
i'm glad i did bc i never would have ordered it/'s

I wrote the following code to replace all the contractions and backslash with empty space and applied it using lambda function. But when I again look at my string I see some forward slash as well and now I can't figure out how to add it inside the replace method because it is not able to take multiple arguments.

def expanded(x):
    if type(x) is str:
        x = x.replace('\', '')
        for key in contractions:
            value = contractions[key]
            x = x.replace(key, value)
        return x
    else:
        return x

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use

x = x.replace('\', '').replace("/","")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...