1

我需要在 python 的数据框中替换部分文本。但是,替换字符串应该从一个大的多词字符串列表中选择。我已经编写了以下简单示例来演示问题以及我使用 for 循环的解决方案。它运行良好,但如果单词列表和数据框很大,则 for 循环运行起来会变得非常昂贵。我想知道是否有任何方法可以避免这里的 for 循环。

    text = ['I am north and west','you are east and south']
    df = pd.DataFrame(text)

    def loop_names(str):
        words = ['north and west','east and south']
        for word in words:
            str = re.sub(r'%s' %re.escape(word),'at location',str)
        return(str)
    df[0] = df[0].apply(loop_names)        
    df

    # Alternatively:

    text = ['I am north and west','you are east and south']
    df = pd.DataFrame(text)
    words = ['north and west','east and south']
    for word in words:
        df[0] = df[0].str.replace(r'%s' %re.escape(word),'at location')
    df

    # Alternatively:

    text = ['I am north and west','you are east and south']
    df = pd.DataFrame(text)
    words = ['north and west','east and south']
    for word in words:     
        df[0] = df[0].apply(lambda x: re.sub(r'%s' %re.escape(word),'at 
    location',x))
    df
4

0 回答 0