简体   繁体   English

分箱极坐标

[英]Binning polar coordinates

My question is quite straight forward.我的问题很直接。 I want to bin polar coordinates, which means that the domain in which I want to bin is limited by 0 and 360, where 0 = 360. Here start my problems, due to this circular behavior of the data, and as I want to bin each 1 degree starting from 0.5 degrees up to 355.5 degrees (Unfortunately due to the nature of the project binning from (0,1] until (359,360]), then, I have to make sure that there is a bin that goes from (355.5,0.5], which is obviously not what will happen by default.我想合并极坐标,这意味着我想要合并的域受 0 和 360 的限制,其中 0 = 360。由于数据的这种循环行为,这里开始我的问题,因为我想合并每 1 度从 0.5 度开始到 355.5 度(不幸的是,由于项目从 (0,1] 到 (359,360]) 分箱的性质,然后,我必须确保有一个从 (355.5) 开始的分箱,0.5],这显然不是默认情况下会发生的。

I made up this script to better illustrate what I am looking for:我编写了这个脚本来更好地说明我在寻找什么:

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)

You will see that if the data is between 355.5 and 0.5 degrees, the binning will be NaN.您将看到,如果数据介于 355.5 和 0.5 度之间,则分箱将为 NaN。 I want to find a solution in which that would be (355.5,0.5]我想找到一个解决方案,即 (355.5,0.5]

So, my result (depending on which seed you set, of course), will look something like this:因此,我的结果(当然取决于您设置的种子)将如下所示:

          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

And I would like it to be:我希望它是:

          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]

Since you cannot have a pandas interval of the form (355.5, 0.5] , you can only have them as string:由于您不能拥有(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]'})
)

Output:输出:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM