I have a dataframe (df) containing names with abbreviations like below:
Name
ABC CO
XYZ CO LTD
S.A.L.P, S.P.A.
XXX L.P
NUR YER SAN.TIC.LTD
BAAB TERMINALS LTD.
I have to replace the abbreviations with their complete words referring to a list. So Below was my approach
import pandas as pd
repl = {'CO' : 'COMPANY','LTD' : 'LIMITED','L.P' : 'LIMITED PARTNERSHIP','LTD.' : 'LIMITED','.LTD' : 'LIMITED'}
repl = {rf'\b{k}\b': v for k, v in repl.items()}
df2 = df['Name'].replace(repl, regex=True)
df2
Below is the output;
0 ABC COMPANY
1 XYZ COMPANY LIMITED
2 S.A.LIMITED PARTNERSHIP, S.P.A.
3 XXX LIMITED PARTNERSHIP
4 NUR YER SAN.TIC.LTD
5 BAAB TERMINALS LIMITED.
Name: Name, dtype: object
here S.A.L.P must not replaced with L.P
Expected output :
0 ABC COMPANY
1 XYZ COMPANY LIMITED
2 S.A.L.P, S.P.A.
3 XXX LIMITED PARTNERSHIP
4 NUR YER SAN.TIC.LIMITED
5 BAAB TERMINALS LIMITED.
Name: Name, dtype: object
The code should replace L.P with LIMITED PARTNERSHIP only when it is present separately as a different string not when it is a part of some string. Can anyone help me out with the issue please. Thanks.