Home > Back-end >  Loop over dataset and classify positions (x,y) of IDs according to a predefined area
Loop over dataset and classify positions (x,y) of IDs according to a predefined area

Time:03-11

I would like to classify my "IDs" according to their x y positions within a predefined zone, predefined as:

bottom_left = (500, 100) 
top_right = (700, 200)

My df looks like this:

    date                    id  x   y
0   2022-02-28 18:46:18.640 5   573 16
1   2022-02-28 18:46:18.902 5   585 43
2   2022-02-28 18:46:19.113 5   596 76
3   2022-02-28 18:46:19.360 5   609 107
4   2022-02-28 18:46:19.593 5   612 142
5   2022-02-28 18:46:19.839 5   619 178
6   2022-02-28 18:46:20.070 5   619 207
7   2022-02-28 18:46:20.312 5   619 238
8   2022-02-28 18:46:20.548 5   619 270
9   2022-02-28 18:46:20.790 5   615 295

How do I create a new column "zone", 0= IDs in my zone, 1=ID outside of my zone.

Any help is highly appreciated.

CodePudding user response:

Use:

(x0, y0), (x1, y1) = bottom_left, top_right
df['zone'] = (df['x'].between(x0, x1) & df['y'].between(y0, y1)).astype(int)
print(df)

# Output
                      date  id    x    y  zone
0  2022-02-28 18:46:18.640   5  573   16     0
1  2022-02-28 18:46:18.902   5  585   43     0
2  2022-02-28 18:46:19.113   5  596   76     0
3  2022-02-28 18:46:19.360   5  609  107     1
4  2022-02-28 18:46:19.593   5  612  142     1
5  2022-02-28 18:46:19.839   5  619  178     1
6  2022-02-28 18:46:20.070   5  619  207     0
7  2022-02-28 18:46:20.312   5  619  238     0
8  2022-02-28 18:46:20.548   5  619  270     0
9  2022-02-28 18:46:20.790   5  615  295     0
  • Related