Skip to main content
 首页 » 编程设计

python之PANDAS 在一列字符串中找到确切的单词和单词(多个)之前的单词并附加它,python 中的新列

2025年05月04日26Free-Thinker

<分区>

数据框看起来像这样

col_a 
Python PY is a general purpose PY language 
 
Programming PY language in Python PY  
 
Its easier to understand  PY 
 
The syntax of the language is clean PY 

此代码我已尝试实现此功能但无法获得预期的输出。如果有任何帮助表示赞赏。

下面是我用正则表达式处理的代码:

df['col_a'].str.extract(r"([a-zA-Z'-]+\s+PY)\b") 

期望的输出:

col_a                                       col_b_PY      
Python PY is a general purpose language         Python PY purpose PY 
Programming PY language in Python PY            Python PY Programming PY      
Its easier to understand  PY                    understand PY  
The syntax of the language is clean PY          clean  PY