Spell Checking a column in Python/Pandas/PyEnchant
<p>I'm trying to use Pyenchant to spell check each entry in a column called pets in a pandas dataframe called house.</p>

<pre><code>import enchant
dict = enchant.Dict("en_US")

for pets in house:
     [pets] = dict.suggest([pets])[0]
</code></pre>

<p>When I run this code, I get an error about not passing bytestrings to Pyenchant. Not sure what to do. Full error text below:</p>

<blockquote>
  <p>File "myfile", line 20, in 
      [pets] = dict.suggest([pets])[0]
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/enchant/<strong>init</strong>.py", line 662, in suggest
      word = self._StringClass(word)
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/enchant/utils.py", line 152, in <strong>new</strong>
      raise Error("Don't pass bytestrings to pyenchant")
  enchant.errors.Error: Don't pass bytestrings to pyenchant</p>
</blockquote>

<p>How can I fix this? Thanks.</p>

<p>If your dataframe contains bytestrings, you will need to decode them before you pass them to <code>enchant</code>; you can do this with <code>.str.decode('utf-8')</code>. Then to apply your function, the cleanest way to approach this type of situation is usually to use <code>map</code> across your Series rather than iterating. (Also you shouldn't shadow the keyword <code>dict</code>):</p>

<pre><code>checker =  enchant.Dict("en_US")
house = pd.Series([b'caat', b'dogg'])

#decode the bytestrings
house = house.str.decode('utf-8')

#map the spell checker
house.map(lambda x: checker.suggest(x)[0])

# Out[19]:
# 0    cat
# 1    dog
# dtype: object
</code></pre>

python pandas pyenchant