【问题标题】:how to group data in bins of 1 degree latitude?如何在纬度 1 度的 bin 中对数据进行分组?
【发布时间】:2019-08-06 15:16:17
【问题描述】:

我有一些地理数据(全球)作为数组:

  • 纬度:lats = ([34.5,34.2,67.8,-24,...])
  • 风速:u = ([2.2,2.5,6,-3,-0.5,...])

我想说明风速如何取决于纬度。因此,我想将数据分箱在 1 度的纬度箱中。

latbins = np.linspace(lats.min(),lat.(max),180)

我如何计算哪些风速落在哪个箱中。我读到了 pandas.groupby。这是一个选项吗?

【问题讨论】:

  • 看看pd.cut

标签: python pandas group-by binning


【解决方案1】:

numpy 函数np.digitize 执行此任务。

这里有一个对 bin 中的每个值进行分类的示例:

import numpy as np
import math

# Generate random lats
lats = np.arange(0,10) - 0.5
print("{:20s}: {}".format("Lats", lats))
# Lats                : [-0.5  0.5  1.5  2.5  3.5  4.5  5.5  6.5  7.5  8.5]

# Generate bins spaced by 1 from the minus to max values of lats
bins = np.arange(math.floor(lats.min()), math.ceil(lats.max()) +1, 1)
print("{:20s}: {}".format("Bins", bins))
# Bins                : [-1  0  1  2  3  4  5  6  7  8  9]

lats_bins = np.digitize(lats, bins)
print("{:20s}: {}".format("Lats in bins", lats_bins))
# Lats in bins        : [ 1  2  3  4  5  6  7  8  9 10]

正如 cmets 中的@High Performance Mark 所建议的,由于您想在 1 度的 bin 中拆分,您可以使用 floor 提取每个 lats 的地板(注意:如果有负值,此方法会引入负索引箱):

lats_bins_floor = np.floor(lats)
# lats_bins_floor = lats_bins_floor + abs(min(lats_bins_floor))
print("{:20s}: {}".format("Lats in bins (floor)", lats_bins_floor))
# Lats in bins (floor): [-1.  0.  1.  2.  3.  4.  5.  6.  7.  8.]

【讨论】:

    猜你喜欢
    • 2015-02-11
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2014-01-22
    • 2017-06-15
    相关资源
    最近更新 更多