我有一个数据帧,格式如下:
W1 W2 W3 W4 W5 W6 W7 W8
0 0 1 0 1 1 1 1
0 0 1 0 0 1 1 1
0 1 0 0 1 1 0 0
1 0 0 0 1 1 0 1
有一个参数DIFF=3。 我在每一行中查找从W1到W4的列,并搜索最后的1。它将位于第W3、W3、W2、W1列。随后,我将整行1右侧的下3个(DIFF)元素更改为0。 例如,我用x标记了这些元素:
W1 W2 W3 W4 W5 W6 W7 W8
0 0 1 x x x 1 1
0 0 1 x x x 1 1
0 1 x x x 1 0 0
1 x x x 1 1 0 1
最终结果:
W1 W2 W3 W4 W5 W6 W7 W8
0 0 1 0 0 0 1 1
0 0 1 0 0 0 1 1
0 1 0 0 0 1 0 0
1 0 0 0 1 1 0 1
现在,我有一个非常复杂的解决方案,它使用iterrows()
,但我正在寻找一个泛弹性的解决方案。你知道吗
用途:
解释:
每行使用^{} :
通过} 通信:
>=1
与^{再次
cumsum
通过boolen mask:通过
2,3,4
比较下3个值,并首先忽略:如果要定义
n
和DIFF
值,请使用更动态的解决方案:以下是函数方法和泛弹性方法的混合:
请注意,此解决方案将删除原始数据框中的数据
相关问题 更多 >
编程相关推荐