How to remove square brackets and add each word into different columns

Viewed 25

I have a CSV file that contains multiple values in it like:

[text] [text2] [text3] [text4] [text5] [text6]
[text] [text2] [text3] [text4] [text5] [text6]
[text] [text2] [text3] [text4] [text5] [text6]
[text] [text2] [text3] [text4] [text5] [text6]

I want to remove all square brackets and store these values in different columns. Can anyone help me with this?

1 Answers

Assuming your file looks like the following:

import pandas as pd
data = [['[text]','[text]','[text]','[text]'],
        ['[text]','[text]','[text]','[text]'],
        ['[text]','[text]','[text]','[text]']]
t = pd.DataFrame(data)
t

File

You can use the given code snippet:

t.apply(lambda x: x.str.strip('[|]'))

End Result

Explanation:

  • .apply() function applies a function to every element of data frame
  • lambda x: x.str.strip('[|]') this function strip '[' or ']' from left or right of a string.
Related