找到该点所属的范围?

2024-05-29 05:55:37 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑一些钻孔中单个测试的数据帧tests

  borehole  depthTest
0      B-1        1.5
1      B-1       20.0
2     B-42        1.0
3     B-42        2.0
4     B-42       15.0
5     B-42       30.0
6     B-09        1.0
7     B-09       10.0
8     B-09       15.0

我有另一个数据框liths,每个钻孔的岩性范围:

  borehole  depthTop   lith
0      B-1         0   sand
1      B-1         5   clay
2      B-1        18  shale
3     B-42         0   sand
4     B-42         1   clay
5     B-42        26  shale
6     B-09         0   sand
7     B-09        12  shale

岩性是每个钻孔的连续序列。例如:在B-1中,砂的深度为0至5 m,粘土的深度为5至18 m,页岩的深度为18 m以上。每种岩性的底部就是下一种岩性的顶部。换句话说,每种岩性的底部都是liths.groupby('borehole').depthTop.shift(-1)

编辑:我想加入两个df,这样我就可以得到每个测试的岩性:我想在borehole上进行匹配,然后找到最接近depthTop<;=depthTestlith。你知道吗

例如:在B-42中,粘土的深度在1 m到26 m之间。在B-42中15.0 m处的试验应归类为粘土,因为15在1到26之间。你知道吗

下面是期望的结果:

  borehole  depthTest   lith
0      B-1        1.5   sand
1      B-1       20.0  shale
2     B-42        1.0   clay
3     B-42        2.0   clay
4     B-42       15.0   clay
5     B-42       30.0  shale
6     B-09        1.0   sand
7     B-09       10.0   sand
8     B-09       15.0  shale

这看起来像是一个groupbymerge_asof问题,但我不知道如何将它们结合起来。你知道吗

到目前为止,我的解决方案是将其转储到sqlite3,然后进行between连接(就像我做的here),但这看起来确实是失败。你知道吗


Tags: 数据tests序列groupby钻孔岩性claysand
2条回答

让我们试试这个:

tests['lith'] = tests.groupby('borehole')['depthTest'].transform(lambda x: pd.cut(x,
       bins = liths.loc[liths.borehole == x.name,'depthTop'].values.tolist() + [np.inf],
       labels=liths.loc[liths.borehole == x.name,'lith'], right=False))

输出:

  borehole  depthTest   lith
0      B-1        1.5   sand
1      B-1       20.0  shale
2     B-42        1.0   clay
3     B-42        2.0   clay
4     B-42       15.0   clay
5     B-42       30.0  shale
6     B-09        1.0   sand
7     B-09       10.0   sand
8     B-09       15.0  shale

让我们使用pd.cut来标记范围内的值。
通过groupby从liths数据框中逐孔得到适合的binslabels。你知道吗

df2上查找最接近的值并分类:

def logic(k):
    vals = df2.loc[df2.borehole == k.borehole,:]
    return vals[vals.depthTop == max(vals[vals.depthTop <= k.depthTest].depthTop)].lith.item()

df.transform(logic, 1)


0     sand
1    shale
2     clay
3     clay
4     clay
5    shale
6     sand
7     sand
8    shale

相关问题 更多 >

    热门问题