Pandas extract numbers from column into new columns

后端 未结 5 2329
谎友^
谎友^ 2020-12-21 05:51

I currently have this df where the rect column is all strings. I need to extract the x, y, w and h from it into separate columns. The dataset is very large so I need an effi

5条回答
  •  自闭症患者
    2020-12-21 05:56

    Use str.extract, which extracts groups from regex into columns:

    df['rect'].str.extract(r'\((?P\d+),(?P\d+)\),(?P\d+) by (?P\d+)', expand=True)
    

    Result:

         x    y    w    h
    0  120  168  260  120
    1  120  168  260  120
    2  120  168  260  120
    3  120  168  260  120
    4  120  168  260  120
    

提交回复
热议问题