0

Hi i need to replace a string,'Manik+/India/IAM/" from the data in my column ID of data frame df. I need to replace this string with blank, i.e. nothing.

df['ID'] = df['ID'].str.replace('Manik+/India/IAM/', '') doesnt work because i believe the thing i want to replace has both letters and +/.

what is the best way to achieve this?

enter image description here

1 Answers1

0

If you always have "something, then /, then the numerical ID", use rsplit:

df['ID'] = df['ID'].str.rsplit('/', n=1).str[-1]

Else, ensure you're not using a regex as + has a special meaning in regex:

df['ID'] = df['ID'].str.replace('Manik+/India/IAM/', '', regex=False) 
mozway
  • 194,879
  • 13
  • 39
  • 75