将某些列更改为零

Change certain columns into zeros

我有一个非常大的数据集 XposMay (125,800 000)。为了方便起见,我在下面制作了较小的版本。我想查看 SomAprilMay 中的哪些值小于 3,并将相应的列删除为零。这是我下面的代码,它不会将任何内容更改为零。

SomAprilMay=[0,0,0,1,0,1,2,3,4,15,12,14,1,10]
XposMay=[[50,51,52,53,54,55,56,57,58,59,60,61,62,63],
         [50,51,52,53,54,55,56,57,58,59,60,61,62,63],
         [50,51,52,53,54,55,56,57,58,59,60,61,62,63]]
Xpos1May=XposMay
a=[]
b=[]
for k in range (0,len(SomAprilMay)):
    if SomAprilMay[k] < 3:
        a.append(SomAprilMay[k])
        b.append(k)
for m in range (0,len(XposMay)):
       Xpos1May[:][b[m]]=0

因为 SomAprilMay 的前 7 个和最后一个元素 < 3,所以想要的结果是:

Xpos1May = [[0,0,0,0,0,0,0,57,58,59,60,61,62,0],
            [0,0,0,0,0,0,0,57,58,59,60,61,62,0],
            [0,0,0,0,0,0,0,57,58,59,60,61,62,0]]

我该怎么做?

我建议为此任务使用 numpy 数组,因为这比遍历整个过程要快。但是,SomAprilMay 和 XposMay 列表的维度不相等,所以我假设你在那里输入错误并在最后一个 1 之前添加了另一个 14。此代码

import numpy as np
SomAprilMay=np.array([0,0,0,1,0,1,2,3,4,15,12,14,14,1])
XposMay=np.array([[50,51,52,53,54,55,56,57,58,59,60,61,62,63],
         [50,51,52,53,54,55,56,57,58,59,60,61,62,63],
         [50,51,52,53,54,55,56,57,58,59,60,61,62,63]])
XposMay.T[SomAprilMay < 3] = 0
XposMay

然后生成所需的

array([[ 0,  0,  0,  0,  0,  0,  0, 57, 58, 59, 60, 61, 62,  0],
       [ 0,  0,  0,  0,  0,  0,  0, 57, 58, 59, 60, 61, 62,  0],
       [ 0,  0,  0,  0,  0,  0,  0, 57, 58, 59, 60, 61, 62,  0]])

您的 SomAprilMay 有 13 个元素,所以我添加了一个。

这是一个衬垫:

SomAprilMay=[0,0,0,1,0,1,2,3,4,5,15,12,14,1]

XposMay=[[50,51,52,53,54,55,56,57,58,59,60,61,62,63],
         [50,51,52,53,54,55,56,57,58,59,60,61,62,63],
         [50,51,52,53,54,55,56,57,58,59,60,61,62,63]]

mask = [e < 3 for e in SomAprilMay]

Xpos1May = [[0 if mask[i] else item for i, item in enumerate(sub) ] for sub in XposMay]