【问题标题】:Binning polar coordinates合并极坐标
【发布时间】:2020-02-12 23:23:00
【问题描述】:

我的问题很直接。我想分箱极坐标,这意味着我要分箱的域受 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]

【问题讨论】:

  • 显示示例输入和输出
  • df['Bins'] = pd.cut(df['Points'].add(0.5)%360, bins=np.arange(360))?

标签: python pandas


【解决方案1】:

由于您不能拥有(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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多