Basudev
Basudev

Reputation: 135

replace the text in one column with a dictionary in other column

I have texts in one column and respective dictionary in another column. I have tokenized the text and want to replace those tokens which found a match for the key in respective dictionary. the text and and the dictionary are specific to each record of a pandas dataframe.

import pandas as pd

data =[['1','i love mangoes',{'love':'hate'}],['2', 'its been a long time we have not met',{'met':'meet'}],['3','i got a call from one of our friends',{'call':'phone call','one':'couple of'}]]

df = pd.DataFrame(data, columns = ['id', 'text','dictionary']) 

The final dataframe the output should be

data =[['1','i hate mangoes'],['2', 'its been a long time we have not meet'],['3','i got a phone call from couple of of our friends']
df = pd.DataFrame(data, columns =['id, 'modified_text'])

I am using Python 3 in a windows machine

Upvotes: 1

Views: 175

Answers (2)

Horace
Horace

Reputation: 1054

I added spaces to the key and values to distinguish a whole word from part of it:

def replace(text, mapping):
    new_s = text
    for key in mapping:
        k = ' '+key+' '
        val = ' '+mapping[key]+' '
        new_s = new_s.replace(k, val)
    return new_s

df_out = (df.assign(modified_text=lambda f: 
                    f.apply(lambda row: replace(row.text, row.dictionary), axis=1))
          [['id', 'modified_text']])

print(df_out)
  id                                     modified_text
0  1                                    i hate mangoes
1  2              its been a long time we have not met
2  3  i got a phone call from couple of of our friends

Upvotes: 0

anky
anky

Reputation: 75080

You can use dict.get method after zipping the 2 cols and splitting the sentence:

df['modified_text']=([' '.join([b.get(i,i) for i in a.split()]) 
                  for a,b in zip(df['text'],df['dictionary'])])
print(df)

Output:

id                                  text  \
0  1                        i love mangoes   
1  2  its been a long time we have not met   
2  3  i got a call from one of our friends   

                                   dictionary  \
0                            {'love': 'hate'}   
1                             {'met': 'meet'}   
2  {'call': 'phone call', 'one': 'couple of'}   

                                      modified_text  
0                                    i hate mangoes  
1             its been a long time we have not meet  
2  i got a phone call from couple of of our friends

Upvotes: 1

Related Questions