简体   繁体   中英

Python: Concatenate string separated by comma in pandas series

After using a TextBlob spell corrector, the sentence in each row becomes separated by comma.

from textblob import TextBlob
list = df['sentence'].tolist()

def TBSpellCorrector(sentence):
    b = TextBlob(sentence)
    return b.correct()

df['corrected_sentence']=df['sentence'].apply(TBSpellCorrector)

Result:

    sentence         corrected_sentence
132 on fre     (o, n,, f, i, r, e)             
35  beautful    (b, e, a, u, t, i, f, u, l)    

I need to concatenate the sentence separated by comma.

Expected Output
    sentence         corrected_sentence        corrected_sentence2
132 on fre           (o, n,, f, i, r, e)             on fire
35  beautful    (b, e, a, u, t, i, f, u, l)         beautiful

if the correct_sentence is in a list form, you can join them with join

>>> sent = ['o','n',' ','f','i','r','e']
>>> ''.join(sent)
'on fire'

The .correct() method returns a textblob.blob.TextBlob object. You just need to cast it to a string, or access its .string property:

from textblob import TextBlob
import pandas as pd

def TBSpellCorrector(sentence):
    return TextBlob(sentence).correct().string # <<< See here

df = pd.DataFrame({'sentence':['on fre','beautful']})
df['sentence'].apply(TBSpellCorrector)
# 0       on are
# 1    beautiful
# Name: sentence, 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