Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas: replace column value with keys and values in a dictionary of list values

I have a dataframe and a dictionary as follows (but much bigger),

import pandas as pd
df = pd.DataFrame({'text': ['can you open the door?','shall you write the address?']})

dic = {'Should': ['can','could'], 'Could': ['shall'], 'Would': ['will']}

I would like to replace the words in the text column if they can be found in dic list of values, so i did the following and it works for the lists that have one value but not for the other list,

for key, val in dic.items():
    if df['text'].str.lower().str.split().map(lambda x: x[0]).str.contains('|'.join(val)).any():
       df['text'] = df['text'].str.replace('|'.join(val), key, regex=False)
print(df)

my desired output would be,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

              text
0   Should you open the door?
1  Could you write the address?

>Solution :

You can use lowercase in flatten dictionary to d for keys and values, then replace values with words boundaries and last use Series.str.capitalize:

d = {x.lower(): k.lower() for k, v in dic.items() for x in v}


regex = '|'.join(r"\b{}\b".format(x) for x in d.keys())
df['text'] = (df['text'].str.lower()
                        .str.replace(regex, lambda x: d[x.group()], regex=True)
                        .str.capitalize())
print(df)
                           text
0     Should you open the door?
1  Could you write the address?
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading