1

I have the following data:

print(df):
Name
James (C)
Mick
Tash (C)
Liv
Nathan
Chris

I am simply trying to get

print(df):
James
Mick
Tash
Liv
Nathan
Chris

I have tried:

df['Name'] = df['Name'].str.replace(' (C)','')

Which does nothing. and then i also try:

df['Name'] = df['Name'].replace({'(C)': ''}, inplace=True)

Which clears the whole column on output.

Then I tried:

df['Name'] = df['Name'].str.replace("[(C)]", "")

Which works but it removes any C eg Chris becomes hris

I have looked and these are the main options but they all don't quite work?

2 Answers 2

6

Use \ for escape regex, because () are special regex characters:

df['Name'] = df['Name'].str.replace(' \(C\)','')
print (df)
     Name
0   James
1    Mick
2    Tash
3     Liv
4  Nathan
5   Chris
Sign up to request clarification or add additional context in comments.

2 Comments

great! thanks very much i think that is working. I also have a name that has Jason (C,5) so would i need to use more ` \ ` in that case too?
@SOK - then use df['Name'] = df['Name'].str.replace(' \(.*\)','') - removing all values in ()
1

You could pass regex=False to turn off regex ... () are special characters in regex and str.replace works on regex by default :

df['Name'].str.replace(' (C)','', regex=False)

0     James
1      Mick
2      Tash
3       Liv
4    Nathan
5     Chris
Name: Name, dtype: object

2 Comments

thanks @sammywemmy. The first answer I got worked but i tried this and it also works so I appreciate the answer!
not a problem... @jezrael's solution allows u to extend to more use cases. My aim was just to show why your initial attempt did not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.