我有两个数据帧Overall
和df2
。
总体上
Time ID_1 ID_2
2020-02-25 09:24:14 140209 81625000
2020-02-25 09:24:14 140216 91625000
2020-02-25 09:24:18 140219 80250000
2020-02-25 09:24:18 140221 90250000
25/02/2020 09:42:02 143982 39075000
df2
ID_1 ID_2 Time Match?
140209 81625000 25/02/2020 09:24:14 no_match
143983 44075000 25/02/2020 09:42:02 no_match
143982 39075000 25/02/2020 09:42:02 match
143984 39075000 25/02/2020 09:42:02 no_match
我想检查df2
是否存在于Overall
中,同一行的df2.Match?
是否存在匹配。如果是,则返回一个新列,表示是,如果不表示匹配,则返回否
我试过了
Overall_1 = pds.merge(Overall, df2, on=….., how='left', indicator= 'Exist')
Overall_1.drop([...], inplace = True, axis =1 )
Overall_1['Exist']= np.where((Overall_1.Exist =='both') & (Overall_1.Match? == match), 'yes', 'no')
但错误占了上风
TypeError: Cannot perform 'rand_' with a dtyped [bool] array and scalar of type [float]
因此,生成的Overall_1
数据帧应该如下所示:
Time ID_1 ID_2 Exist
2020-02-25 09:24:14 140209 81625000 No
2020-02-25 09:24:14 140216 91625000 NaN
2020-02-25 09:24:18 140219 80250000 NaN
2020-02-25 09:24:18 140221 90250000 Nan
25/02/2020 09:42:02 143982 39075000 Yes
您可以尝试: df_diff=pd.concat([总体,df2])。删除重复项(keep=False)
使用
merge
和np.select.
或者更简单地使用
replace
dict和.merge
相关问题 更多 >
编程相关推荐