我试图在数据帧中查找模式并进行替换
我正在寻找的一个模式示例:
[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
您的代码中有两个小错误。您需要将
\w
更改为\w+
,并使替换模式成为原始字符串如果没有加号,
\w
将只匹配一个字符相关问题 更多 >
编程相关推荐