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: first two elements in a string column matches dictionary key

I have a dataframe as follows:

import pandas as pd

df = pd.DataFrame({'data1':['the weather is nice today','This is interesting','the weather is good'],
             'data2':['It is raining','The plant is green','the weather is sunny']})

and I have a dictionary as follows:

my_dict = {'the weather':'today','the plant':'tree'}

I would like to replace the first two words in the data2 column if they are found in the dictionary key. I have done the following:

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

for old, new in dic.items():    
    if pd.Series([' '.join(map(str, l)) for l in df['data2'].str.lower().str.split().map(lambda x: x[0:2])]).str.contains('|'.join(old.capitalize()).any():
       df['data2'] = df['data2'].str.replace(old, new.capitalize(), regex=False)
    else:
       print('does not exist')

but when i print(df), nothing has been replaced.

the expected output:

                       data1                 data2
0  the weather is nice today         It is raining
1        This is interesting    Tree is green
2        the weather is good    Today is sunny

>Solution :

If I understand correctly, this is one way to do it (there may be more efficient ways):

df.data2 = df.data2.str.lower()
for k in my_dict:
  df.data2 = df.data2.str[:len(k)].replace(k, my_dict[k]) + df.data2.str[len(k):]

df.data2 = df.data2.str.capitalize()

Lowercasing and capitalization weren’t in your question but were part of your code, so I put them in (otherwise it would fail because the capitalization doesn’t match in your sample code).

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