根据其他条件循环遍历数据框以1和0填充新列

时间:2019-02-27 20:04:45

标签: python pandas

transaction    startdate    

1 0 1 0

1 个答案:

答案 0 :(得分:1)

检查get_indexervalues切片

s=df.filter(like='/')
idcol=s.columns.get_indexer(df.TransDate)
df['PreviousDayDummy']=(s.values[np.arange(len(s)),idcol-1]==df.lookup(df.index,df.TransDate)).astype(int) 

df
Out[72]: 
   Transaction TransDate  Cashier  PreviousDayDummy  01/01  01/02  01/03
0            1     01/02        2                 1      1      1      1
1            2     01/02        1                 0      0      1      1
2            3     01/02        2                 1      1      1      1
3            4     01/03        3                 0      0      0      1