【问题标题】:Down sample DF1 according to the coordinates in DF2根据DF2中的坐标下采样DF1
【发布时间】:2020-05-13 18:32:22
【问题描述】:

我有两个 DataFrame。两者都有 X 和 Y 坐标。但 DF1 比 DF2 密集得多。我想根据 DF2 中的 X Y 坐标对 DF1 进行下采样。具体来说,对于 DF2 中的每个 X/Y 对,我选择 X +/-delta 和 Y +/-delta 之间的 DF1 数据,并计算 Z 的平均值。New_DF1 将具有与 DF2 相同的 X Y 坐标,但具有平均值通过下采样得到 Z 值。

以下是我为此目的而制作的一些示例和函数。我的问题是对于大型数据集来说太慢了。如果有人对矢量化操作而不是粗略的循环有更好的想法,我们将不胜感激。

创建数据示例:

DF1 = pd.DataFrame({'X':[0.6,0.7,0.9,1.1,1.3,1.8,2.1,2.8,2.9,3.0,3.3,3.5],"Y":[0.6,0.7,0.9,1.1,1.3,1.8,2.1,2.8,2.9,3.0,3.3,3.5],'Z':[1,2,3,4,5,6,7,8,9,10,11,12]})
DF2 = pd.DataFrame({'X':[1,2,3],'Y':[1,2,3],'Z':[10,20,30]})

功能:

def DF1_match_DF2_target(half_range, DF2, DF1):
    ### half_range, scalar, define the area of dbf target
    ### dbf data
    ### raw pwg pixel map
    DF2_X =DF2.loc[:,["X"]]
    DF2_Y =DF2.loc[:,['Y']]
    results = list()
    for i in DF2.index:
        #Select target XY from DF2
        x= DF2_X.at[i,'X']
        y= DF2_Y.at[i,'Y']

        #Select X,Y range for DF1
        upper_lmt_X = x+half_range
        lower_lmt_X = x-half_range
        upper_lmt_Y = y+half_range
        lower_lmt_Y = y-half_range

        #Select data from DF1 according to X,Y range, calculate average Z
        subset_X = DF1.loc[(DF1['X']>lower_lmt_X) & (DF1['X']<upper_lmt_X)]
        subset_XY = subset_X.loc[(subset_X['Y']>lower_lmt_Y) & (subset_X['Y']<upper_lmt_Y)]
        result = subset_XY.mean(axis=0,skipna=True)
        result[0] = x #set X,Y in new_DF1 the same as the X,Y in DF2
        result[1] = y #set X,Y in new_DF1 the same as the X,Y in DF2
        results.append(result)
    results = pd.DataFrame(results)
    return results

测试和结果:

new_DF1 = DF1_match_DF2_target(0.5,DF2,DF1)
new_DF1

Test and Result

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用 'pandas:cut()' 函数如何使用边界值进行聚合?

    half_range = 0.5
    # create bins
    x_bins = [0] + list(df2.x)
    y_bins = [0] + list(df2.y)
    tmp = [half_range]*(len(df2)+1)
    
    x_bins = [a + b for a, b in zip(x_bins, tmp)]
    y_bins = [a + b for a, b in zip(y_bins, tmp)]
    
    key = pd.cut(df1.x, bins=x_bins, right=False, precision=1)
    df3 = df1.groupby(key).mean().reset_index()
    df2.z = df3.z
    
    df2
    x   y   z
    0   1   1   3.0
    1   2   2   6.5
    2   3   3   9.5
    

    【讨论】:

    • 感谢您的评论!我对 python 和 pandas 很陌生,所以学习这个 pandas.cut 函数很棒。但是,有几个问题。 1) 如果我们使用半范围作为 0.3,那么 bin 变为 [0.3,1.3), [1.3, 2.3), [2.3, 3.3)。虽然期望值是 [0.7,1.3)、[1.7, 2.3) 等。2) New_DF1 中的 X、Y 坐标也是平均值,我希望在 DF2 中具有相同的 X、Y 坐标。有没有办法在 New_DF1 中替换 X,Y?
    • Q1:我认为'pandas:cut'函数只能支持连续的段值。这里的分类值指定为'[0.5, 1.5, 2.5, 3.5]'。 Q2:我想修改一个已经提交的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多