Search code examples
pythonpandastext-mining

pandas - join words after specific word


I have a pandas dataframe with a column of comments in the form

import pandas as pd
df = pd.DataFrame({'comment':['aaa bbb ccc not verb ddd']}) 
df.loc[0,'comment']

'aaa bbb ccc not verb ddd'

I want to join together the not with the word after it, in the example verb as not_verb, and return the rest of the row as is:

'aaa bbb ccc not_verb ddd'

Any help is appreciated.

EDIT:

Basically I'd like to join from " not " to the end of the word following it.


Solution

  • Use str.replace:

    df.comment.str.replace(r'\b(not\s)', 'not_')
    

    Output:

    0    aaa bbb ccc not_verb ddd
    Name: comment, dtype: object