Home > Mobile >  Removing [] and "" from a pandas column
Removing [] and "" from a pandas column

Time:12-04

I have a column in a pandas df:

["text","text2"]
["bla","bla","bla"]

What's the best way to remove those [] and "" to get:

text,text2
bla,bla,bla

Thanks

CodePudding user response:

df.column.apply(lambda x: ','.join(x).replace('"',''))

CodePudding user response:

This would achieve the substitutions you need - using re.sub:

import re
import pandas as pd

df = pd.DataFrame({"c1":["[\"text\",\"text2\"]","[\"bla\",\"bla\",\"bla\"]"]})

df["c1"].apply(lambda x:re.sub('[\["\]]', "", x))

OUTPUT

0     text,text2
1    bla,bla,bla
  • Related