Python 如何删除列表中出现的指定单词
我有一个数据框,第一列中的文本名为“原始列”。Python 如何删除列表中出现的指定单词,python,pandas,list,dataframe,split,Python,Pandas,List,Dataframe,Split,我有一个数据框,第一列中的文本名为“原始列”。 我已经成功地从带有列表的文本列“original_column”中选择了特定的单词,并使用以下代码将它们附加到另一列并从原始列中删除: list1 = {’text’ , ‘and’ , ‘example’} finder = lambda x: next(iter([y for y in x.split() if y in list1]), None) df['list1'] = df.original_column.apply(finder
我已经成功地从带有列表的文本列“original_column”中选择了特定的单词,并使用以下代码将它们附加到另一列并从原始列中删除:
list1 = {’text’ , ‘and’ , ‘example’}
finder = lambda x: next(iter([y for y in x.split() if y in list1]), None)
df['list1'] = df.original_column.apply(finder)
df['original column']=df['original column'].replace(regex=r'(?i)'+ df['list1'],value="")
现在,我想在这段代码的基础上,在将列表中的单词添加到新列之后,只能够从“原始_列”中删除列表中特定单词的第一个实例数据帧当前看起来如下所示:
| original column |
__________________________
| text text word |
--------------------------
| and other and |
我当前的代码输出以下内容:
| original column | list1
______________________________
| word | text
------------------------------
| other | and
我希望输出以下内容:
| original column | list1
_______________________________
| text word | text
-------------------------------
| other and | and
让我们做
替换
df['original column']=df['original column'].replace(regex=r'(?i)'+ df['list1'],value="")
df
Out[101]:
original column list1
0 text text word
1 text text and
假设给定的数据帧为:
df = pd.DataFrame({"original_column": ["text text word", "text and text"]})
使用:
这将导致数据帧df
为:
original_column list1
0 text text word
1 text text and
original_column list1
0 text text word
1 text text and