如何在Python中从DataFrame列中删除特定的变化文本?

问题描述 投票:0回答:1

我有一个包含列'test'的数据框。它看起来像这样。

Column Test 
'[ABC: 814.6] text text text text [text:123]'
'[ABC: 432.9] text text [ABC: 433] text text [text:123]'
'[ABC: 1] text text text [342:] text [text:123]'

我想删除所有的 '[ABC:XXX.X]' 部件。我知道如何替换 "静态 "文本,就像这样,但是由于XXX.X正在改变,我不知道如何解决这个问题。

df['Test_New'] = df['Test'].str.replace("[ABC: XXX.X]", '')

但是由于XXX. X在变化,我不知道如何解决这个问题。

希望的输出。

Column Test 
' text text text text [text:123]'
' text text  text text [text:123]'
' text text text [342:] text [text:123]'

非常感谢!

python string dataframe replace re
1个回答
1
投票

按照@ZaxR的评论。str.replace 支持regex。

df['Test_New'] = df['Test'].str.replace(r"\[ABC: [\d]{1,3}(?:.\d)?\]", '')
© www.soinside.com 2019 - 2024. All rights reserved.