Search code examples
pythonpandasdataframenlp

Pandas Replace part of string with value from other column


I have a dataframe (see example, because sensitive data) with a full term (string), a text snippet (one big string) containing the full term and an abbreviation (string). I have been struggling with how to replace the full term in the text snippet with the corresponding abbrevation. Can anyone help? Example:

    term                      text_snippet                                 abbr
0   aanvullend onderzoek      aanvullend onderzoek is vereist om...        ao/

So I want to end up with:

    term                      text_snippet                                 abbr
0   aanvullend onderzoek      ao/ is vereist om...                         ao/

Solution

  • You can use apply and replace terms with abbrs:

    df['text_snippet'] = df.apply(
        lambda x: x['text_snippet'].replace(x['term'], x['abbr']), axis=1)
    
    df
    

    Output:

                       term          text_snippet abbr
    0  aanvullend onderzoek  ao/ is vereist om...  ao/