NumPy:如何使用重复项进行左连接的数组

3
使用Cython时,我需要将使用Pandas的df1.merge(df2, how='left')转换为纯NumPy代码,但是我发现numpy.lib.recfunctions.join_by(key, r1, r2, jointype='leftouter')不支持key上的任何重复值。有没有解决这个问题的办法?

2
大多数recfunctions中的基本思想是定义一个新的dtype,创建适当的“空”数组,并按字段名称复制值。这都是可读的Python代码;没有隐藏的编译代码。如果现有函数无法完成任务(它们没有被广泛使用或测试),请编写自己的函数。 - hpaulj
1个回答

2
这是一个尝试使用纯粹的numpy左连接处理重复键的方法:
import numpy as np

def join_by_left(key, r1, r2, mask=True):
    # figure out the dtype of the result array
    descr1 = r1.dtype.descr
    descr2 = [d for d in r2.dtype.descr if d[0] not in r1.dtype.names]
    descrm = descr1 + descr2 

    # figure out the fields we'll need from each array
    f1 = [d[0] for d in descr1]
    f2 = [d[0] for d in descr2]

    # cache the number of columns in f1
    ncol1 = len(f1)

    # get a dict of the rows of r2 grouped by key
    rows2 = {}
    for row2 in r2:
        rows2.setdefault(row2[key], []).append(row2)

    # figure out how many rows will be in the result
    nrowm = 0
    for k1 in r1[key]:
        if k1 in rows2:
            nrowm += len(rows2[k1])
        else:
            nrowm += 1

    # allocate the return array
    _ret = np.recarray(nrowm, dtype=descrm)
    if mask:
        ret = np.ma.array(_ret, mask=True)
    else:
        ret = _ret

    # merge the data into the return array
    i = 0
    for row1 in r1:
        if row1[key] in rows2:
            for row2 in rows2[row1[key]]:
                ret[i] = tuple(row1[f1]) + tuple(row2[f2])
                i += 1
        else:
            for j in range(ncol1):
                ret[i][j] = row1[j]
            i += 1

    return ret

基本上,它使用一个普通的 dict 来执行实际的连接操作。与 numpy.lib.recfunctions.join_by 类似,这个函数也会返回一个掩码数组。当右侧数组中缺少键时,这些值将在返回数组中被屏蔽。如果您更喜欢记录数组(其中所有缺失数据都设置为0),则只需在调用 join_by_left 时传递 mask=False


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接