如何在句子的 pandas 列中使用自动更正 [英] How to use autocorrect in Pandas column of sentences
问题描述
我有一列句子,我像这样拆分
I have a column of sentences, which i am splitting like so
df['ColTest'] = df['ColTest'].str.lower().str.split()
我想做的是遍历每个句子中的每个单词并应用autocorrect.spell()
What i am trying to do is loop through each word in each sentence and apply the autocorrect.spell()
for i in df['ColTest']:
for j in i:
df['ColTest'][i][j].replace(at.spell(j))
这引发了错误
AttributeError:浮动"对象没有属性替换"
AttributeError: 'float' object has no attribute 'replace'
自动拼写自动拼写
DataFrame看起来像
DataFrame looks like
ColTest
This is some test string
that might contain a finger
but this string might contain a toe
and this hass a spel error
我的栏中没有数字...有什么想法吗?
There are no numbers in my column...any ideas please?
推荐答案
使用自动更正库,您需要遍历数据框的行,然后遍历给定行中的单词以应用spell
方法.这是一个工作示例:
Using the autocorrect library, you need to iterate through the rows of the dataframe then iterate through the words within a given row to apply the spell
method. Here's a working example:
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
:
Also as suggested by @jpp in the comment below, we can avoid using lambda
as follows:
df["colTest"] = [' '.join([spell(i) for i in x.split()]) for x in df['colTest']]
输入内容如下:
Here's how the input looks like:
colTest
0 and this hass a spel error
输出:
Output:
0 and this has a spell error
Name: colTest, dtype: object
这篇关于如何在句子的 pandas 列中使用自动更正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!