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

How to add a new string element to the Series of strings

I have a pandas Series of strings (job titles). I also have a string with a "target" job title.

My task is to iterate over the Series and check if the "target" is present in the elements of the Series. It must be an exact match (including white spaces).

If it is present then the element of the Series must be replaced with the "target". If there is no match then the original element will stay unchanged.
I must return and an adjusted Series.

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

Here is my code and errors.

def nn(s, target)

      sn = pd.Series()

      for w in s:  
          if target in w:
              sn.append(pd.Series(target))
        
          else:
              sn.append(pd.Series(w))
return sn

INPUT data:

import pandas as pd
import copy

s = pandas.Series(['DATA ANALYTIC SCIENTIST',
                   'BEST DATA SCIENTIST',
                   'DATA SCIENTIST',
                   'DATA SCIENTIST - SPACE OPTIMIZATION',
                   'SCIENTIST DATA'])

target = 'DATA SCIENTIST'

nn(s, target)

ERRORS (empty Series)

Series([], dtype: float64)

Thank you!!

>Solution :

You want str.contains and mask:

s.mask(s.str.contains(target), target)

Or extract then fillna:

s.str.extract(f'({target})', expand=False).fillna(s)

Output:

0    DATA ANALYTIC SCIENTIST
1             DATA SCIENTIST
2             DATA SCIENTIST
3             DATA SCIENTIST
4             SCIENTIST DATA
dtype: object
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