簡體   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