繁体   English   中英

迭代列的行并删除 python 中特定单词之后的所有文本

[英]iterate rows of a column and remove all text after specific words in python

如果在一行中找到特定的单词组合,python 中是否有办法删除所有文本? 我有六种不同的单词组合,之后应该删除文本(“来自制造商”就是一个例子)。 我想遍历一列的行并删除在这些单词之后找到的所有文本。

list_of_words = ['Descrizione Prodotto', 'Produktbeschreibung des Herstellers', 'Description du fabricant', 'From the manufacturer', 'Descripción Prodotto']
var = df['info']
for index, row in df.iterrows():
    for word in list_of_words:
        if word in var:
            var.split(word, 1)[0]

使用 lambda function 尝试以下操作:

list_of_words = ['Descrizione Prodotto', 'Produktbeschreibung des Herstellers', 'Description du fabricant', 'From the manufacturer', 'Descripción Prodotto']

def clear(x):
    for i in list_of_words:
        if i in x:
            x=x[:x.find(i)+len(i)]
    return x

df['info']=df['info'].apply(lambda x: clear(x))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM