使用Pandas groupby迭代和修改数据帧

时间:2017-06-03 16:04:14

标签: python arrays pandas numpy

我正在使用1的大数组,需要从数组的各个部分系统地删除0。大数组由许多较小的数组组成,对于每个较小的数组,我需要系统地用0代替它的上下三角形。例如,我们有一个由索引值指示的5个子数组的数组(所有子数组具有相同的列数):

     0    1    2
0  1.0  1.0  1.0
1  1.0  1.0  1.0
1  1.0  1.0  1.0
2  1.0  1.0  1.0
2  1.0  1.0  1.0
2  1.0  1.0  1.0
3  1.0  1.0  1.0
3  1.0  1.0  1.0
3  1.0  1.0  1.0
3  1.0  1.0  1.0
4  1.0  1.0  1.0
4  1.0  1.0  1.0
4  1.0  1.0  1.0
4  1.0  1.0  1.0
4  1.0  1.0  1.0

我希望在其上下三角形中修改每组行,以便得到的矩阵为:

      0    1    2
0  1.0  1.0  1.0
1  1.0  1.0  0.0
1  0.0  1.0  1.0
2  1.0  0.0  0.0
2  0.0  1.0  0.0
2  0.0  0.0  1.0
3  1.0  0.0  0.0
3  1.0  1.0  0.0
3  0.0  1.0  1.0
3  0.0  0.0  1.0
4  1.0  0.0  0.0
4  1.0  1.0  0.0
4  1.0  1.0  1.0
4  0.0  1.0  1.0
4  0.0  0.0  1.0

目前我只使用numpy来实现这个结果数组,但我想我可以使用Pandas分组加快速度。实际上,我的数据集非常大,几乎有500,000行。 numpy代码如下:

import numpy as np

candidateLengths = np.array([1,2,3,4,5])
centroidLength =3

smallPaths = [min(l,centroidLength) for l in candidateLengths]

# This is the k_values of zeros to delete. To be used in np.tri
k_vals = list(map(lambda smallPath: centroidLength - (smallPath), smallPaths))
maskArray = np.ones((np.sum(candidateLengths), centroidLength))

startPos = 0
endPos = 0
for canNo, canLen in enumerate(candidateLengths):
    a = np.ones((canLen, centroidLength))
    a *= np.tri(*a.shape, dtype=np.bool, k=k_vals[canNo])
    b = np.fliplr(np.flipud(a))
    c = a*b

    endPos = startPos + canLen

    maskArray[startPos:endPos, :] = c

    startPos = endPos

print(maskArray)

当我在我的真实数据集上运行它时,执行需要将近5-7秒。我认为这取决于这个庞大的循环。如何使用pandas分组来获得更高的速度?感谢

1 个答案:

答案 0 :(得分:1)

新答案

def tris(n, m):
    if n < m:
        a = np.tri(m, n, dtype=int).T
    else:
        a = np.tri(n, m, dtype=int)
    return a * a[::-1, ::-1]

idx = np.append(df.index.values, -1)
w = np.append(-1, np.flatnonzero(idx[:-1] != idx[1:]))
c = np.diff(w)
df * np.vstack([tris(n, 3) for n in c])

     0    1    2
0  1.0  1.0  1.0
1  1.0  1.0  0.0
1  0.0  1.0  1.0
2  1.0  0.0  0.0
2  0.0  1.0  0.0
2  0.0  0.0  1.0
3  1.0  0.0  0.0
3  1.0  1.0  0.0
3  0.0  1.0  1.0
3  0.0  0.0  1.0
4  1.0  0.0  0.0
4  1.0  1.0  0.0
4  1.0  1.0  1.0
4  0.0  1.0  1.0
4  0.0  0.0  1.0

旧答案

我定义了一些辅助三角函数

def tris(n, m):
    if n < m:
        a = np.tri(m, n, dtype=int).T
    else:
        a = np.tri(n, m, dtype=int)
    return a * a[::-1, ::-1]

def tris_df(df):
    n, m = df.shape
    return pd.DataFrame(tris(n, m), df.index, df.columns)

然后

df * df.groupby(level=0, group_keys=False).apply(tris_df)

     0    1    2
0  1.0  1.0  1.0
1  1.0  1.0  0.0
1  0.0  1.0  1.0
2  1.0  0.0  0.0
2  0.0  1.0  0.0
2  0.0  0.0  1.0
3  1.0  0.0  0.0
3  1.0  1.0  0.0
3  0.0  1.0  1.0
3  0.0  0.0  1.0
4  1.0  0.0  0.0
4  1.0  1.0  0.0
4  1.0  1.0  1.0
4  0.0  1.0  1.0
4  0.0  0.0  1.0
相关问题