0

I have a dataframe with points given in two columns x and y.

Thing  x  y  length_x  length_y
0     A  1  3         1         2
1     B  2  3         2         1

These (x,y) points are situated in the middle of one of the sides of a rectangle with vertex lengths length_x and length_y. What I wish to do is for each of these points give the coordinates of the rectangles they are on. That is: the following coordinated for Thing A would be:

(1+1*0.5, 3), (1-1*0.5,3), (1+1*0.5,3-2*0.5), (1-1*0.5, 3-2*0.5)

The half comes from the fact that the given lengths are the middle-points of an object so half the length is the distance from that point to the corner of the rectangle.

Hence my desired output is:

Thing  x  y  Corner_x  Corner_y  length_x  length_y
0     A  1  3       1.5       2.0         1         2
1     A  1  3       1.5       1.0         1         2
2     A  1  3       0.5       2.0         1         2
3     A  1  3       0.5       1.0         1         2
4     A  1  3       1.5       2.0         1         2
5     B  2  3       3.0       3.0         2         1
6     B  2  3       3.0       2.5         2         1
7     B  2  3       1.0       3.0         2         1
8     B  2  3       1.0       2.5         2         1
9     B  2  3       3.0       3.0         2         1

I tried to do this with defining a lambda returning two value but failed. Tried even to create multiple columns and then stack them, but it's really dirty.

bb = []
for thing in list_of_things:
    new_df = df[df['Thing']=='{}'.format(thing)]
    
    df = df.sort_values('x',ascending=False)
    df['corner 1_x'] = df['x']+df['length_x']/2
    df['corner 1_y'] = df['y']
    df['corner 2_x'] = df['x']+1df['x_length']/2
    df['corner 2_y'] = df['y']-df['length_y']/2
        .........

Note also that the first corner's coordinates need to be repeated as I later what to use geopandas to transform each of these sets of coordinates into a POLYGON.

What I am looking for is a way to generate these rows is a fast and clean way.

4
  • shouldn't the y corners for A be 2 and 4? Commented Sep 6, 2021 at 12:08
  • No. The point from which the calculations are made is not in the center of the rectangle. Commented Sep 6, 2021 at 12:59
  • OK, then you have to slightly adapt my answer, I misunderstood this point Commented Sep 6, 2021 at 13:10
  • 1
    Doesn't matter. A accepted the answer because the procedure is there. The rest is tweaking. Commented Sep 6, 2021 at 13:59

1 Answer 1

2

You can use apply to create your corners as lists and explode them to the four rows per group.

Finally join the output to the original dataframe:

df.join(df.apply(lambda r: pd.Series({'corner_x': [r['x']+r['length_x']/2, r['x']-r['length_x']/2],
                                      'corner_y': [r['y']+r['length_y']/2, r['y']-r['length_y']/2],
                                     }), axis=1).explode('corner_x').explode('corner_y'),
        how='right')

output:

  Thing  x  y  length_x  length_y corner_x corner_y
0     A  1  3         1         2      1.5        4
0     A  1  3         1         2      1.5        2
0     A  1  3         1         2      0.5        4
0     A  1  3         1         2      0.5        2
1     B  2  3         2         1        3      3.5
1     B  2  3         2         1        3      2.5
1     B  2  3         2         1        1      3.5
1     B  2  3         2         1        1      2.5
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.