1

我是熊猫和附魔的新手。我想使用python检查短句中的拼写。

我有一个熊猫数据框:

id_num      word
1           live haapy
2           know more
3           ssweam good
4           eeat little
5           dream alot

我想用“检查”列实现下一个表</p>

id_num         word           check
1              live haapy     True, False
2              know more      True, True
3              ssweam good    False, True
4              eeat little    False, True
5              dream alot     True, False

做这个的最好方式是什么?

我试过这段代码:

import enchant
dic = enchant.Dict("ru_Eng")
df['list_word'] = df['word'].str.split() #Get list of all words in each sentence using split()
row = list()
for row in df[['id_num', 'list_word']].iterrows():
r = row[1]
for word in r.list_word:
    rows.append((r.id_num, word))
df2 = pd.DataFrame(rows, columns=['id_num', 'word']) #Make the table with id_num column and a column of separate words

然后我得到了新的数据框(df2):

id_num    word
1         live
1         haapy
2         know
2         more
3         ssweam
3         good
4         eeat
4         little
5         dream
5         alot

之后,我使用以下方法检查单词:

column = df2['word'] for i in column: n = dic.check(i) print(n)

结果是:

True
False
True
True
False
True
False
True
True
False

检查是否正确执行,但是当我尝试将此结果放入新的 pandas 数据框列时,我得到了所有单词的所有 False 值。

for i in column:
df2['res'] = dic.check(i)

结果数据框:

id_num    word    res
1         live   False
1         haapy  False
2         know   False
2         more   False
3         ssweam False
3         good   False
4         eeat   False
4         little False
5         dream  False
5         alot   False

我将不胜感激任何帮助!

4

0 回答 0