[英]Binning polar coordinates
我的問題很直接。 我想合並極坐標,這意味着我想要合並的域受 0 和 360 的限制,其中 0 = 360。由於數據的這種循環行為,這里開始我的問題,因為我想合並每 1 度從 0.5 度開始到 355.5 度(不幸的是,由於項目從 (0,1] 到 (359,360]) 分箱的性質,然后,我必須確保有一個從 (355.5) 開始的分箱,0.5],這顯然不是默認情況下會發生的。
我編寫了這個腳本來更好地說明我在尋找什么:
bins_direction = np.linspace(0.5,360.5,360, endpoint = False)
points = np.random.rand(10000)*360
df = pd.DataFrame({'Points': points})
df['Bins'] = pd.cut(x= df['Points'],
bins=bins_direction)
您將看到,如果數據介於 355.5 和 0.5 度之間,則分箱將為 NaN。 我想找到一個解決方案,即 (355.5,0.5]
因此,我的結果(當然取決於您設置的種子)將如下所示:
Points Bins
0 17.102993 (16.5, 17.5]
1 97.665600 (97.5, 98.5]
2 46.697548 (46.5, 47.5]
3 9.832000 (9.5, 10.5]
4 21.260980 (20.5, 21.5]
5 47.433179 (46.5, 47.5]
6 359.813283 nan
7 355.654251 (355.5, 356.5]
8 0.23740105 nan
我希望它是:
Points Bins
0 17.102993 (16.5, 17.5]
1 97.665600 (97.5, 98.5]
2 46.697548 (46.5, 47.5]
3 9.832000 (9.5, 10.5]
4 21.260980 (20.5, 21.5]
5 47.433179 (46.5, 47.5]
6 359.813283 (359.5, 0.5]
7 355.654251 (355.5, 356.5]
8 0.23740105 (359.5, 0.5]
由於您不能擁有(355.5, 0.5]
形式的pandas
區間,因此您只能將它們作為字符串:
bins = [0] + list(np.linspace(0.5,355.5,356)) + [360]
df = pd.DataFrame({'Points': [0,1,350,356, 357, 359]})
(pd.cut(df['Points'], bins=bins, include_lowest=True)
.astype(str)
.replace({'(-0.001, 0.5]':'(355.5,0.5]', '(355.5, 360.0]':'(355.5,0.5]'})
)
輸出:
0 (355.5,0.5]
1 (0.5, 1.5]
2 (349.5, 350.5]
3 (355.5,0.5]
4 (355.5,0.5]
5 (355.5,0.5]
Name: Points, dtype: object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.