简体   繁体   中英

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.

Use str.replace :

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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