I have a dataframe df that looks like this:
Company Name ID
0 Finl Corp 111
1 Fund Tr 222
2 Invt Fd 333
3 Govt Fd 444
4 Trinity Inc 555
And I'm replacing strings that are commonly abbreviated:
df['Company Name'] = df['Company Name'].str.replace('Finl', 'Financial')
df['Company Name'] = df['Company Name'].str.replace('Tr', 'Trust')
df['Company Name'] = df['Company Name'].str.replace('Invt', 'Investment')
df['Company Name'] = df['Company Name'].str.replace('Fd', 'Fund')
df['Company Name'] = df['Company Name'].str.replace('Govt', 'Government')
But how do I replace Tr with Trust only when Tr is the last two characters of a string, like row 1 (but not row 4)?
Can I do something like?
df['Company Name'] = df['Company Name'].str.endswith(' Tr').replace(' Tr', ' Trust') so that the output is:
Company Name ID
0 Financial Corp 111
1 Fund Trust 222
2 Investment Fund 333
3 Government Fund 444
4 Trinity Inc 555