简体   繁体   中英

Escape special characters in Dataframe Pandas Python

Input: a dataframe with special characters: "’" in some of the values.

Output: search for this string "’" and replace it with "'"

Example: I execute this code but it cannot search for special characters

import pandas as pd
df = {"Name": ["abc’ company", "dfe’ company"]}
df = pd.DataFrame(data=df)
for i in df["Name"]:
   if "’" in i:
      j = i.replace("’", "'")
      df.replace(to_replace=i, value=j)

Just try pass regex

df = df.replace({'’':"'"}, regex=True)
           Name
0  abc' company
1  dfe' company

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM