Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correct spelling in a Pandas DataFrame

Using the TextBlob library it is possible to improve the spelling of strings by defining them as TextBlob objects first and then using the correct method.

Example:

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

Is it possible to do this to strings in a Pandas DataFrame series such as this one:

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

To return this:

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

Either using TextBlob or some other method.

like image 767
RDJ Avatar asked Jan 28 '16 19:01

RDJ


Video Answer


1 Answers

You could do something like:

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

Using pandas.Series.apply.

like image 173
Ami Tavory Avatar answered Sep 20 '22 02:09

Ami Tavory