Replace row strings in dataframe with appropriate words in another pandas data frame

I have a df that has 1 column

List 0 What are you trying to achieve 1 What is your purpose right here 2 When students don't have a proper foundation 3 I am going to DESCRIBE a sunset 

I have another dataframe df2

which has 2 columns

  original correct 0 are were 1 sunset sunrise 2 I we 3 right correct 4 is was 

I want to replace such words in my df, which appears in the original column of my df2 and replace them with the corresponding words in the correct column. and save new lines in another df_new data df_new

Is this possible without using loops and iterations and only using the simple pandas concept?

ie my df_new should contain.

  List 0 What were you trying to achieve 1 What was your purpose correct here 2 When students don't have a proper foundation 3 we am going to DESCRIBE a sunrise 

Also this is just a test case, MY df MAY CONTAIN millions of lines of a line, and therefore my df2, What will be the most efficient solution path that I can continue on?

+6
source share
1 answer

One of many possible solutions:

 In [371]: boundary = r'\b' ...: ...: df.List.replace((boundary + df2.orignal + boundary).values.tolist(), ...: df2.correct.values.tolist(), ...: regex=True) ...: Out[371]: 0 What were you trying to achieve 1 What was your purpose correct here 2 When students don't have a proper foundation 3 we am going to DESCRIBE a sunrise Name: List, dtype: object 
+2
source

Source: https://habr.com/ru/post/1014829/


All Articles