Rename columns with ranges based on dictionary

Issue

I have this dataframe:

df = pd.DataFrame( {'an2': {0: 'f', 1: 'i', 2: '', 3: '', 4: 'f', 5: 'c,f,i,g', 6: 'c,d,e,g'}} )

which yields to:

    an2
0   f
1   i
2   
3   
4   f
5   c,f,i,g
6   c,d,e,g

I would like to create new column df[‘an3’] by renaming df[‘an2’] according to the following dictionary:

dic = {'a': 'john', 
'b': 'paul', 
'c': 'mike',
'd': 'elephant',
'e': 'water', 
'f': 'bread', 
'g': 'julie',
'h': 'anna', 
'i': 'mauricio',
'j': 'claudia'}

Therefore desired output is:

    an2      an3
0   f        bread
1   i        mauricio
2       
3       
4   f        bread
5   c,f,i,g  mike,bread,mauricio,claudia
6   c,d,e,g  mike,elephant,water,claudia

I tried using dictionary above with the following code

df['an3'] = df['fan2'].replace(dic)

unfortunatley it only work for those cells where one single entry was found on df[‘an2’]

Solution

Let us try Series.replace:

df['an2'].replace({fr'\b{k}\b': v for k, v in dic.items()}, regex=True)

0                        bread
1                     mauricio
2                             
3                             
4                        bread
5    mike,bread,mauricio,julie
6    mike,elephant,water,julie
Name: an2, dtype: object

Answered By – Shubham Sharma

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.