0

I have columns in pandas data frame that look like:

Variable Names    
name1 [a]

name2 [b]

name3 [c]

I am using the following code to remove all the square brackets and everything within those brackets from all the variable names. This is the code I am using:

import re 
df = df.rename(columns=lambda n: n.replace(r'[\d+.*', r''))

This code is not working. How can I modify this code to get it to work?

Thanks!

1 Answer 1

1

This should do what you are looking for.

df.rename(columns=lambda x: re.sub("[\[].*?[\]]", "", x))
Sign up to request clarification or add additional context in comments.

1 Comment

Please accept the answer if it works for you. Thanks

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.