Python 在dataframe中查找模式并进行替换

Python 在dataframe中查找模式并进行替换,python,pandas,Python,Pandas,我试图在数据帧中查找模式并进行替换 我正在寻找的一个模式示例: [不是]+[任何东西]+[更多] 不再(不再=>模式)=>不再 数据: 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.

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

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

[不是]+[任何东西]+[更多]

不再(不再=>模式)=>不再

数据:

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
我试过:

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
我的输出:

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
输出良好:

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

您的代码中有两个小错误。您需要将
\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

您的代码中有两个小错误。您需要将
\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