我试图根据两个条件创建一个目标变量。我有X值是二进制的,X2值也是二进制的。我的条件是,当nVer X从1变为0时,只有在X2中从0变为1时,y中才有1。如果这之后是X中从0到1的变化,那么我们一开始就不做这种变化。我附上了一张excel中的图片
为了解释X的变化,我也做了以下几点
df['X-prev']=df['X'].shift(1)
df['Change-X;]=np.where(df['X-prev']+df['X']==1,1,0)
# this is the data frame
X=[1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]
X2=[0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1]
df=pd.DataFrame()
df['X']=X
df['X2']=X2
然而,这还不够,因为我需要知道X变化之后的第一个变化。我附上了一张例子的照片
保留与转换匹配的行
(X=1, X+1=0)
和(X2=1, X2-1=0)
,然后将所有选定行合并到一个列表中,其中0表示“开始一个周期”,1表示“结束一个周期”但在此列表中,您可以有连续的开始或结束,因此您需要再次筛选以仅获得(0,1)个周期。之后,用原始数据帧索引重新索引这个新系列,并用1填充
相关问题 更多 >
编程相关推荐