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

时间:2020-03-24 20:58:53

标签: python pandas

我尝试过:

library(dplyr)
library(stringr)
df %>% 
   mutate(new_B = str_replace_all(B, str_replace(A, " ", "|"), ''))

但是我得到了错误:from autocorrect import Speller spell = Speller(lang='en') df['Text'] = df['Text'].apply(lambda x: spell(x))

1 个答案:

答案 0 :(得分: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)