Skip to content
Advertisement

elegant way to replace multiple list of values with a multiple single value

I have a dataframe like as shown below

JavaScript

I would like to replace a list of values like as shown below

a) Replace P, PRIMARY,PRI with primary b) Replace S, SECONDARY, SEC with secondary c) Replace T, TERTIARY, THIRD with third

I tried the below

JavaScript

But is there any other efficient and elegant way to write this in a single line?

I expect my output to be like as shown below

JavaScript

Advertisement

Answer

One idea for avoid multiple replace is use dictionary and flatten it to another dict for keys from lists values, for match convert column to uppercase by Series.str.upper:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement