pythonpandastextblob

Extract Sentences from review column and adding it in a new column, repeating the other rows for each new sentence


I have 3 columns. Review, Date and Review Rating. I want to split reviews into sentences and add the sentences under a new column, but the other rows should repeat based on the number of sentences.

for example

Date          Review_Rating           Review                      Sentence
12-02-2021        5          ram is good. ram is intelligent       ram is good.
12-02-2021        5          ram is good. ram is intelligent       ram is intelligent

Solution

  • IIUC, you can use assign and explode:

    >>> df
    
             Date  Review_Rating                           Review
    0  12-02-2021              5  ram is good. ram is intelligent
    
    
    >>> df.assign(Sentence=df['Review'].str.split(r'\.\s*')).explode('Sentence')
    
             Date  Review_Rating                           Review            Sentence
    0  12-02-2021              5  ram is good. ram is intelligent         ram is good
    0  12-02-2021              5  ram is good. ram is intelligent  ram is intelligent