Search code examples
pythonpandasstringsplitseries

Remove brackets from around quotation marks in string


d = {'1': (None,None), '2': ("ZRH","CGN"), '3': ("VIE","LAX")}
  ser = pd.Series(data=d, index=['1', '2', '3'])
  print(ser)
        
  pd.Series(ser, dtype="string")
  patn = re.sub(r"[\([{})\]]", "", ser)
  strippedText = str(ser).replace('(','').replace(')','')
  ser = ser.str.split(',', 1).str

I have the following problem. I Have a pandas series, where the strings are in quatation marks ("ZRH","CGN") and i want to separate the panda series into two new columns without the brackets and the quotation marks. The problem is that the () are not inside the "" and therefor wont be removed.

My desired output is two columns one with ZRH and one with CGN. The code above is a sample and what tried so far. The only output I get is:

col 1 -> ("ZRH"
col 2 -> "CGN")

Solution

  • IIUC, try:

    >>> pd.DataFrame(ser.tolist()).add_prefix("col")
       col0  col1
    0  None  None
    1   ZRH   CGN
    2   VIE   LAX