Skip to content
Advertisement

escaping square brackets from string in dataframe

I have a dataframe where i am try to clean the data from all cols. There are some annomalies in teh data like this: "[n], [ta], [cb]" basically anything in square brackets i want to ignore and replace with a space. I have this:

JavaScript

this works except I still get the square brackets in the data but they are just empty. Not sure how to also remove the square brackets as well as the letters in it. Also not sure if there is a quicker way to do this on all columns and not just one at a time.

Advertisement

Answer

It works for me. I think the reason why you previously got remaining square brackets was because you didn’t include the escape character (back slash).

JavaScript

Without escape characters:

JavaScript

With escape characters:

JavaScript

To apply this to all columns, just use a for loop:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement