I would like to use a lambda function on a whole dataframe column with a conditional to:
- remove the 3rd character
- replace the 4th & 3rd last characters with a single dash '-'
- only when a value starts with 'AB'
Such that 'AB123456789' becomes 'AB2345-89'
df = pd.DataFrame({'key':['123456789','AB123456789','CD123456789','987654321'],'adj_key':['123456789','AB123456789','CD123456789','987654321']})
df['adj_key'] = df['adj_key'].apply(lambda x: (delete 3rd character & replace 4th & 3rd last character with a single dash) if (value begins with 'AB'))
result:
key adj_key
0 123456789 123456789
1 AB123456789 AB2345-89
2 CD123456789 CD123456789
3 987654321 987654321
Cheers
straccessor available in Pandas. It is just more convenient.