Pandas np.在哪里搜索整个列
我有一个这种格式的数据帧Pandas np.在哪里搜索整个列,pandas,numpy,dataframe,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
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
谢谢大家! 如果两列的数据类型相同:
df['Exist'] = df['Date'].isin(df['D1']).astype(np.int8)
如果两列的数据类型相同:
df['Exist'] = df['Date'].isin(df['D1']).astype(np.int8)