如何将拼写器从自动更正应用到数据帧的特定列

2024-06-25 22:44:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试过这个:

from autocorrect import Speller

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

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


Tags: lambdatextfromimportdflangstring错误
1条回答
网友
1楼 · 发布于 2024-06-25 22:44:11

可能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)

相关问题 更多 >