【发布时间】:2020-06-04 08:22:08
【问题描述】:
我正在尝试将引号“”中的每个单词替换为大写单词,除了熊猫列中单词“then”之后的单词: 例如:
0 There was a "quick" "brown" fox who "jumped" over the wall then "fell" and broke its "tooth"
输出应该是:
0 There was a "QUICK" "BROWN" fox who "JUMPED" over the wall then "fell" and broke its "TOOTH"
虽然我能够找到引号中的单词,但我无法排除紧跟在“then”之后的单词。
df.str.replace({r'"(.*?)"':r'\U$1') #this will select and replace all values in quotes to uppercase also values after then
请帮忙。
【问题讨论】:
-
你可以试试
df.str.replace({r'(?<!then\s)"(\w*?)"':r'\U$1') -
非常感谢...它奏效了。能否请您提及我可以研究更多关于正则表达式的资源?谢谢
-
我已将我的建议添加为下面的代码以及我认为有用的链接。希望这些对你也有帮助。您是否还可以更新您最终用于实现结果的确切
df.str.replace代码?我可以使用我输入的正则表达式找到单词,但我无法按照您的方式替换它。所以,如果你能说得出来,我也会学到一些东西:-)
标签: python regex pandas replace