Remove opening and closing parenthesis with word in pandas

前端 未结 4 917
庸人自扰
庸人自扰 2021-01-26 01:07

Given a data frame:

df = 

                         multi
0 MULTIPOLYGON(((3 11, 2 33)))
1 MULTIPOLYGON(((4 22, 5 66)))

I was trying to remov

4条回答
  •  野性不改
    2021-01-26 01:50

    You can use df.column.str in the following way.

    df['a'] = df['a'].str.findall(r'[0-9.]+')
    df = pd.DataFrame(df['a'].tolist())
    print(df)
    

    output:

         0     1
    0  3.49  11.10
    1  4.49  22.12
    

    This will work for any number of columns. But in the end you have to name those columns.

    df.columns = ['a'+str(i) for i in range(df.shape[1])]
    

    This method will work even when some rows have different number of numerical values. like

    df =pd.DataFrame({'a':['MULTIPOLYGON(((3.49)))' ,'MULTIPOLYGON(((4.49 22.12)))']})
    
         a
     0  MULTIPOLYGON(((3.49)))
     1  MULTIPOLYGON(((4.49 22.12)))
    

    So the expected output is

          0     1
    0   3.49    None
    1   4.49    22.12
    

    After naming the columns using,

    df.columns = ['a'+str(i) for i in range(df.shape[1])]
    

    You get,

          a0    a1
    0   3.49    None
    1   4.49    22.12
    

提交回复
热议问题