Home > database >  Go through Pandas dataframe and remove certain chracters
Go through Pandas dataframe and remove certain chracters

Time:01-05

Let's say I have a data frame of integers as strings. Some values may have brackets in them for example "8[7]".

How would I go through each cell in the data frame and remove any brackets and number in the bracket? For example, if there is a cell "5[6]" change the cell to "5" and do this for every cell in the df.

CodePudding user response:

You can apply regex to the entire df:

df.replace('\[(\w )\]', '', regex=True)

CodePudding user response:

You can use regex

s = 5[6]7
re.sub(r'\[[0-9]*\]', '', s)

or simply using split()

s.split('[')[0]   s.split('[')[1].split(']')[1]
  • Related