pandas不用apply如何实现赋值后再与改变后的值比较
源数据
import pandas as pdimport numpy as np
d={'A':[5,14,12,15,16,10,21,15,18,17],'B':[2,12,8,10,11,12,19,9,28,12],'cond':[False,False,True,False,False,False,True,False,True,False]}
df = pd.DataFrame(d)
df
输出
A B cond0 5 2 False
1 14 12 False
2 12 8 True
3 15 10 False
4 16 11 False
5 10 12 False
6 21 19 True
7 15 9 False
8 18 28 True
9 17 12 False
我尝试用
df['cond']=np.where(df['cond'].shift().eq(True)&df['A'].gt(df['B']),df['cond'].shift(),df['cond'])df
输出
A B cond0 5 2 False
1 14 12 False
2 12 8 True
3 15 10 True
4 16 11 False
5 10 12 False
6 21 19 True
7 15 9 True
8 18 28 True
9 17 12 True
索引3原先是false,现在已经改为true了
索引4我想要跟改变后的索引3做对比,也应符合条件,结果为True才对。
但这里索引4对比的是没改变过的索引3,所以不符合条件,如何才能跟改变后的值做对比。
以上是 pandas不用apply如何实现赋值后再与改变后的值比较 的全部内容, 来源链接: utcz.com/p/938505.html