[英]using dictionary to replace words in sentence in DataFrame
I am trying to replace words in a sentence in dataframe by a dictionary. 我正在尝试用字典替换数据框中句子中的单词。 How can I replace the original dataframe?
如何替换原始数据框?
The dictionary : rep_vocab contains {wrong words: correct words}
dataframe: data_test column['question1'] the sentences column['d5'] contains a set of words which is misspelling in the sentences.i use this column to quickly locate the row of sentences which has wrong words. 字典:rep_vocab包含
{wrong words: correct words}
数据帧:data_test列['question1']句子列['d5']包含一组句子中拼写错误的单词。我使用此列快速定位行错误单词的句子。
My code: 我的代码:
data_test.loc[data_test['d5']!=set()['question1'].replace(rep_vocab,regex=True)
It returns the right result, but the original value in dataframe doesn't change. 它返回正确的结果,但数据帧中的原始值不变。 I tried other ways like use
inplace=True
, but it raised a warning: 我尝试了其他方法,例如使用
inplace=True
,但它发出了警告:
A value is trying to be set on a copy of a slice from a DataFrame.
试图在DataFrame的切片副本上设置一个值。 Try using .loc[row_indexer,col_indexer] = value instead
尝试改用.loc [row_indexer,col_indexer] = value
You can just take the output and insert it (so not doing it inplace). 您可以只获取输出并将其插入(因此不要就地进行输出)。
data_test.loc[data_test['d5']!=set()['question1'] = data_test.loc[data_test['d5']!=set()['question1'].replace(rep_vocab,regex=True)
But please check if you need the condition, You might be able to just type: 但是请检查您是否需要该条件,您也许可以输入:
data_test['question1'] = data_test['question1'].replace(rep_vocab,regex=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.