【问题标题】:Python: Concatenate string separated by comma in pandas seriesPython:在熊猫系列中连接用逗号分隔的字符串
【发布时间】:2020-12-31 12:38:55
【问题描述】:

使用 TextBlob 拼写校正器后,每行中的句子会以逗号分隔。

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

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

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

结果:

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

我需要连接逗号分隔的句子。

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

【问题讨论】:

    标签: python python-3.x regex pandas


    【解决方案1】:

    .correct() 方法返回一个 textblob.blob.TextBlob 对象。您只需要将其转换为字符串,或访问其.string 属性:

    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
    

    【讨论】:

      【解决方案2】:

      如果正确的句子是列表形式,你可以用join加入他们

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

      【讨论】:

        猜你喜欢
        • 2021-10-13
        • 2020-04-04
        • 2017-09-29
        • 1970-01-01
        • 2022-10-18
        • 2021-01-21
        • 1970-01-01
        • 2021-05-18
        • 1970-01-01
        相关资源
        最近更新 更多