0

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.

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:

A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57

1 Answers1

0

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)
Peter Mølgaard Pallesen
  • 1,470
  • 1
  • 15
  • 26
  • Thanks a lot! the second solution works. However, there are more than one million rows of sentences. Some of them do not contain wrong words, so I USE column[D5] to make a slice. So I prefer the first solution! (there may be some mistakes in the code) – jonathanschum Aug 21 '19 at 00:46
  • Try to compare the speed of the two. For readability I would strongly prefer the second. – Peter Mølgaard Pallesen Aug 21 '19 at 07:27