user12690225
user12690225

Reputation:

What is the efficient way of splitting a pandas DataFrame column in 2

I have a pandas DataFrame that looks like this:

               x1y1             x2y2
0    [694.0, 427.0]  [1178.0, 601.0]
1    [621.0, 415.0]   [736.0, 456.0]
2    [551.0, 404.0]   [669.0, 461.0]
3    [514.0, 421.0]   [569.0, 463.0]
4    [181.0, 406.0]   [320.0, 462.0]
5    [738.0, 415.0]   [873.0, 474.0]
6   [1158.0, 446.0]  [1209.0, 513.0]
7    [613.0, 176.0]   [692.0, 272.0]
8      [2.0, 295.0]    [50.0, 368.0]
9    [817.0, 305.0]   [870.0, 373.0]
10  [1130.0, 410.0]  [1174.0, 500.0]
11  [1155.0, 420.0]  [1199.0, 497.0]
12   [990.0, 417.0]  [1053.0, 524.0]
13   [952.0, 409.0]  [1003.0, 515.0]
14   [905.0, 412.0]   [944.0, 503.0]
15    [34.0, 432.0]    [84.0, 485.0]
16    [1091.0, 1.0]   [1172.0, 78.0]
17    [859.0, 49.0]   [975.0, 146.0]
18    [710.0, 76.0]   [827.0, 145.0]
19     [68.0, 62.0]   [181.0, 115.0]
20  [1076.0, 252.0]  [1142.0, 297.0]
21  [1058.0, 298.0]  [1103.0, 372.0]
22   [642.0, 336.0]   [675.0, 366.0]
23   [777.0, 382.0]   [800.0, 408.0]
24   [264.0, 241.0]   [331.0, 292.0]

I want to split it into a DataFrame with x1, y1, x2, y2 as columns efficiently without for loops or iterating over the rows, is there some way to do so?

Upvotes: 0

Views: 31

Answers (1)

jezrael
jezrael

Reputation: 862841

Idea is use numpy.hstack to 2d array and pass to DataFrame constructor:

df3 = pd.DataFrame(np.hstack((df['x1y1'].tolist(), 
                              df['x2y2'].tolist())), columns=['x1', 'y1', 'x2', 'y2'])
print (df3)
        x1     y1      x2     y2
0    694.0  427.0  1178.0  601.0
1    621.0  415.0   736.0  456.0
2    551.0  404.0   669.0  461.0
3    514.0  421.0   569.0  463.0
4    181.0  406.0   320.0  462.0
5    738.0  415.0   873.0  474.0
6   1158.0  446.0  1209.0  513.0
7    613.0  176.0   692.0  272.0
8      2.0  295.0    50.0  368.0
9    817.0  305.0   870.0  373.0
10  1130.0  410.0  1174.0  500.0
11  1155.0  420.0  1199.0  497.0
12   990.0  417.0  1053.0  524.0
13   952.0  409.0  1003.0  515.0
14   905.0  412.0   944.0  503.0
15    34.0  432.0    84.0  485.0
16  1091.0    1.0  1172.0   78.0
17   859.0   49.0   975.0  146.0
18   710.0   76.0   827.0  145.0
19    68.0   62.0   181.0  115.0
20  1076.0  252.0  1142.0  297.0
21  1058.0  298.0  1103.0  372.0
22   642.0  336.0   675.0  366.0
23   777.0  382.0   800.0  408.0
24   264.0  241.0   331.0  292.0

Upvotes: 2

Related Questions