在datafram中查找模式并进行替换

2024-05-20 01:06:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在数据帧中查找模式并进行替换

我正在寻找的一个模式示例:

[not] + [anything] + [more]

not any more (not any more => pattern) => not_any_more

数据:

0    can seem form something like coherent...
1    not any more...
2    is unclear any better deal...
3    Peter won’t start if you don’t sit...
4    is unclear basic conditions any...
Name: Data, dtype: object

我试过:

df['Data'] = df['Data'].str.replace(r'(not|no)(\s)(\w)(\s)(more)', '\1_\3_\5')

我的输出:

0    can seem form something like coherent...
1    not any more...
2    is unclear any better deal...
3    Peter won’t start if you don’t sit...
4    is unclear basic conditions any...
Name: Data, dtype: object

输出良好:

0    can seem form something like coherent...
1    not_any_more...
2    is unclear any better deal...
3    Peter won’t start if you don’t sit...
4    is unclear basic conditions any...
Name: Data, dtype: object

Tags: formdataismorenotanycansomething
1条回答
网友
1楼 · 发布于 2024-05-20 01:06:51

您的代码中有两个小错误。您需要将\w更改为\w+,并使替换模式成为原始字符串

如果没有加号,\w将只匹配一个字符

print(df['Data'].str.replace(r'(not|no)(\s)(\w+)(\s)(more)', r'\1_\3_\5'))
#0    can seem form something like coherent...
#1                             not_any_more...
#2               is unclear any better deal...
#3       Peter won’t start if you don’t sit...
#4          is unclear basic conditions any...
#Name: Data, dtype: object

相关问题 更多 >