np.where搜索整列

时间:2017-12-09 14:23:38

标签: pandas numpy dataframe

我有这种格式的数据框

Date       D1         
12/8/2017  12/7/2017  
12/7/2017  12/2/2017  
12/6/2017  12/1/2017  
12/5/2017  
12/4/2017  

df['Exist'] = np.where(df['Date'] == df['D1'], 1, 0)

上面的代码不起作用,因为它只查看同一行,如何使它搜索整个D1以产生:

Date       D1         Exist
12/8/2017  12/7/2017  0
12/7/2017  12/2/2017  1
12/6/2017  12/1/2017  0
12/5/2017             0
12/4/2017             0

谢谢!

1 个答案:

答案 0 :(得分:2)

如果两列的dtype相同:

df['Exist'] = df['Date'].isin(df['D1']).astype(np.int8)
相关问题