ElaSz
ElaSz

Reputation: 107

Changing elements in pandas series according to other series

I have two pandas series with long names like this:

0           abrakadabra
1           hokus pokus
2           lumio
3           flippendo   
4           avada kedavra

Second with short names:

0           abra
1           kra
2           avada
3           hokus          
4           lalala

The aim is to check if any element from first list contains element from second list. If so, change elemnt from 1 list to approriate element from second list. Desirable aoutput:

0 abra
1 hokus
2 lumio
3 flippendo
4 avada

Below function was created:

def common(serie1,serie2):
    for index1,value1 in enumerate(list(serie1)):
         for index2,value2 in enumerate(list(serie2)):
              if value1 in value2:
                  serie2[index2]=value1
              else:
                  serie2[index2]=value2
    return serie2

For these 2 short series it works perfectly. However for longer ones function does not hold names from serie2 in cases, when value1 is not in value2 , instead put 0 there. I can't find what is wrong with my function.

Upvotes: 2

Views: 70

Answers (1)

jezrael
jezrael

Reputation: 863801

You can use extract by joined all values of s2 by | for regex OR with fillna:

pat = r'({})'.format('|'.join(s2))
#for exact match
#pat = r'(\b{}\b)'.format('|'.join(s2))
s = s1.str.extract(pat, expand=False).fillna(s1)
print (s)
0         abra
1        hokus
2        lumio
3    flippendo
4        avada
Name: A, dtype: object

Upvotes: 2

Related Questions