我是熊猫和附魔的新手。我想使用python检查拼字法。
我有一个熊猫数据框:
id_num word
1 live haapy
2 know more
3 ssweam good
4 eeat little
5 dream alot
我想用“ check”列实现下一张表
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
之后,我使用以下命令检查单词:
列= df2 ['word'] 对于列中的我: n = dic.check(i) 打印(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
感谢您的帮助!