如何将Speller从自动更正应用于数据框的特定列

问题描述 投票:0回答:1

我尝试过:

from autocorrect import Speller

spell = Speller(lang='en')
df['Text'] = df['Text'].apply(lambda x: spell(x))

但是我得到了错误:TypeError: expected string or bytes-like object

python pandas
1个回答
0
投票

[df['Text']中的某些值可能既不是str也不是bytes。试试这个:

from autocorrect import Speller

spell = Speller(lang='en')
df['Text'] = df['Text'].apply(
    lambda x: spell(x) if isinstance(x, str) or isinstance(x, bytes) else x)
© www.soinside.com 2019 - 2024. All rights reserved.