在 pandas 列中替换为 Python 正则表达式
Replace with Python regex in pandas column
a = "Other (please specify) (What were you trying to do on the website when you encountered ^f('l1')^?Â\xa0)"
pandas 列中有许多以“^f”开头并以“^”结尾的值。我需要像下面这样替换它们:
"Other (please specify) (What were you trying to do on the website when you encountered THIS ISSUE?Â\xa0)"
你没有提到你已经尝试过什么,也没有提到你的 DataFrame 的其余部分是什么样的,但这是一个最小的例子:
# Create a DataFrame with a single data point
df = pd.DataFrame(["... encountered ^f('l1')^?Â\xa0)"])
# Define a regex pattern
pattern = r'(\^f.+\^)'
# Use the .replace() method to replace
df = df.replace(to_replace=pattern, value='TEST', regex=True)
输出
0
0 ... encountered TEST? )
a = "Other (please specify) (What were you trying to do on the website when you encountered ^f('l1')^?Â\xa0)"
pandas 列中有许多以“^f”开头并以“^”结尾的值。我需要像下面这样替换它们:
"Other (please specify) (What were you trying to do on the website when you encountered THIS ISSUE?Â\xa0)"
你没有提到你已经尝试过什么,也没有提到你的 DataFrame 的其余部分是什么样的,但这是一个最小的例子:
# Create a DataFrame with a single data point
df = pd.DataFrame(["... encountered ^f('l1')^?Â\xa0)"])
# Define a regex pattern
pattern = r'(\^f.+\^)'
# Use the .replace() method to replace
df = df.replace(to_replace=pattern, value='TEST', regex=True)
输出
0
0 ... encountered TEST? )