1

The dataframe has 2 columns: sentence and list. The requirement is to replace the string in df['sentence'] which is present in df['list'] with string found|present.

from pandas import DataFrame

df = {'list': [['Ford','Mercedes Benz'],['ford','hyundai','toyota'],['tesla'],[]],
        'sentence': ['Ford is less expensive than Mercedes Benz' ,'toyota and hyundai mileage is good compared to ford','tesla is an electric car','toyota too has electric cars']
        }

df = DataFrame(df,columns= ['list','sentence'])

The expected output for df['sentence'] is:

Ford|present is less expensive than Mercedes Benz|present
toyota|present and hyundai|present mileage is good compared to ford|present
tesla|present is an electric car
toyota too has electric cars
2
  • why the last item toyota too has electric cars was not modified? Commented Oct 5, 2019 at 10:10
  • Because the respective list doesn't have any string to replace. It's an empty list. Commented Oct 5, 2019 at 10:11

3 Answers 3

1

With regex replacement:

(cut from IPython interactive session)

In [36]: import re                                                                                          

In [37]: def sub_from_list(row): 
    ...:     if row['list']: 
    ...:         row['sentence'] =  re.sub(r'({})'.format('|'.join(set(row['list']))), r'\1|present', row['s
    ...: entence']) 
    ...:     return row 
    ...:                                                                                                    

In [38]: df.apply(sub_from_list, axis=1)                                                                    
Out[38]: 
                      list                                           sentence
0          [Ford, hyundai]     Ford|present is expensive than hyundai|present
1  [ford, hyundai, toyota]  toyota|present and hyundai|present mileage is ...
2                  [tesla]                   tesla|present is an electric car
3                       []                       toyota too has electric cars
Sign up to request clarification or add additional context in comments.

Comments

0

you can do it using apply function and regex to replace the text inside apply function

import re

df = {'list': [['Ford','Mercedes Benz'],['ford','hyundai','toyota'],['tesla'],[]],
        'sentence': ['Fords is less expensive than Mercedes Benz' ,'toyota and hyundai mileage is good compared to ford','tesla is an electric car','toyota too has electric cars']
        }

df = DataFrame(df,columns= ['list','sentence'])

def replace_values(row):
    if len(row.list)>0:
        pat = r"(\b"+"|".join(row.list) +r")(\b)"
        print(pat)
        row.sentence = re.sub(pat, "\\1|present\\2", row.sentence)
    return row

df.apply(replace_values, axis=1)

3 Comments

The last item (index = 3) is replaced as '|presentt|presento|presenty|presento|presentt|presenta|present |presentt|presento| ..
@ansev /Dev. Thank you. If we had Fords intead of Ford. The word is replaced as Ford|presents. The exact match would be good. Isn't?
The new code doesn't replace Fords but it does replace Ford's as Ford|present's. The hyphen S gets added after present. I am looking for an exact string match.
0

You can use a custom function on the data frame as follows:

Code

import pandas as pd

df = {'list': [['Ford','hyundai'],['ford','hyundai','toyota'],['tesla'],[]],
        'sentence': ['Ford is expensive than hyundai' ,'toyota and hyundai mileage is good compared to ford','tesla is an electric car','toyota too has electric cars']
        }

df = pd.DataFrame(df)

def rep_text(row):
    if not row.list:
        return row
    words = row.sentence.split()
    new_words = [word+'|present' \
    if word in row.list else word\
    for word in words]

    row['sentence'] = ' '.join(new_words)
    return row

df = df.apply(rep_text, axis=1)

Output

                      list                                           sentence
0          [Ford, hyundai]     Ford|present is expensive than hyundai|present
1  [ford, hyundai, toyota]  toyota|present and hyundai|present mileage is ...
2                  [tesla]                   tesla|present is an electric car
3                       []                       toyota too has electric cars

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.