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
Produce a copy
df.assign(**dict(zip('xywh', df.rect.str.findall('\d+').str)))
rect 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
Or just reassign to df
df = df.assign(**dict(zip('xywh', df.rect.str.findall('\d+').str)))
df
rect 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
Modify existing df
df[[*'xywh']] = pd.DataFrame(df.rect.str.findall('\d+').tolist())
df
rect 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