如何在 Pandas 列句子中使用自动更正

How to use autocorrect in Pandas column of sentences

我有一列句子,我是这样拆分的

df['ColTest'] = df['ColTest'].str.lower().str.split()

我想做的是遍历每个句子中的每个单词并应用 autocorrect.spell()

for i in df['ColTest']:
for j in i:
    df['ColTest'][i][j].replace(at.spell(j))

这是一个错误

AttributeError: 'float' object has no attribute 'replace'

自动拼写 autospell

DataFrame 看起来像

ColTest
This is some test string
that might contain a finger
but this string might contain a toe
and this hass a spel error

我的专栏中没有数字...请问有什么想法吗?

使用 autocorrect library,您需要遍历数据帧的行,然后遍历给定行中的单词以应用 spell 方法。这是一个工作示例:

from autocorrect import spell 
import pandas as pd 

df = pd.DataFrame(["and this hass a spel error"], columns=["colTest"])
df.colTest.apply(lambda x: " ".join([spell(i) for i in x.split()]))

另外,正如@jpp 在下面的评论中所建议的,我们可以避免使用 lambda,如下所示:

df["colTest"] = [' '.join([spell(i) for i in x.split()]) for x in df['colTest']]

输入内容如下:

                      colTest
0  and this hass a spel error

输出:

0    and this has a spell error
Name: colTest, dtype: object