我想根據他們在預定義區域內的 xy 位置對我的“ID”進行分類,預定義為:
bottom_left = (500, 100)
top_right = (700, 200)
我的 df 看起來像這樣:
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
如何創建一個新列“區域”,0=我的區域中的 ID,1=我的區域之外的 ID。
非常感謝任何幫助。
uj5u.com熱心網友回復:
采用:
(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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/441539.html
下一篇:numpy多項式解釋
