给定数据框
condition
定义为:
0 [3, 4]
1 [2]
我想要另一个数据帧的第0行第3列和第4列以及第1行第2列设置为0
例如给定另一个数据框
df2
:
1 2 3 4
0 0.275464 0.275404 0.2782 0.273485
1 0.275464 0.275404 0.2782 0.273485
申请
condition
后,我希望df2
成为:
1 2 3 4
0 0.275464 0.275404 0.0000 0.0000
1 0.275464 0.000000 0.2782 0.273485
有没有一种好的方法可以在不迭代各个行的情况下做到这一点?
一个可能的解决方案:
indices = cond.explode(0).reset_index().astype(int).values -1
mask = np.full(df.shape, False)
mask[indices[:, 0], indices[:, 1]] = True
df.values[mask] = 0