编程语言
首页 > 编程语言> > python-如何更正Pandas DataFrame中的拼写

python-如何更正Pandas DataFrame中的拼写

作者:互联网

使用TextBlob库,可以通过先将字符串定义为TextBlob对象,然后再使用正确的方法来改善字符串的拼写.

例:

from textblob import TextBlob
data = TextBlob('Two raods diverrged in a yullow waod and surry I culd not travl bouth')
print (data.correct())
Two roads diverged in a yellow wood and sorry I could not travel both

是否可以对像这样的Pandas DataFrame系列中的字符串执行此操作:

data = [{'one': '3', 'two': 'two raods'}, 
         {'one': '7', 'two': 'diverrged in a yullow'}, 
        {'one': '8', 'two': 'waod and surry I'}, 
        {'one': '9', 'two': 'culd not travl bouth'}]
df = pd.DataFrame(data)
df

    one   two
0   3     Two raods
1   7     diverrged in a yullow
2   8     waod and surry I
3   9     culd not travl bouth

返回此:

    one   two
0   3     Two roads
1   7     diverged in a yellow
2   8     wood and sorry I
3   9     could not travel both

使用TextBlob或其他方法.

解决方法:

您可以执行以下操作:

df.two.apply(lambda txt: ''.join(textblob.TextBlob(txt).correct()))

使用pandas.Series.apply.

标签:textblob,pandas,nlp,python
来源: https://codeday.me/bug/20191119/2033596.html