python - 如何在没有匹配索引的情况下比较来自两个 DF 的两个日期?
问题描述
df1
USERID DATE
1 1/1/2018
1 1/2/2018
1 1/3/2018
2 1/2/2018
2 1/3/2018
3 1/3/2018
df2
USERID DATE
1 1/1/2018
2 1/2/2018
3 1/3/2018
我想比较date
from df2
to df1
that 属于同一个USERID
来判断该行是否df1
也存在于df2
Result:
USERID DATE Exists
1 1/1/2018 True
1 1/2/2018 False
1 1/3/2018 False
2 1/2/2018 True
2 1/3/2018 False
3 1/3/2018 True
我想做相当于
np.where((df1['DATE'] == df2['DATE']), True, False)
但现在返回错误Can only compare identically-labeled Series objects
解决方案
你可以这样做merge
:
# create a new column
df2['Exists'] = True
df3 = pd.merge(df1,df2,on=['USERID','DATE'],how='outer').fillna(False)
USERID DATE Exists
0 1 1/1/2018 True
1 1 1/2/2018 False
2 1 1/3/2018 False
3 2 1/2/2018 True
4 2 1/3/2018 False
5 3 1/3/2018 True
推荐阅读
- javascript - 如何遍历来自 django rest-auth 的错误响应
- linux - 使用 VNC 交付基于订阅的应用程序
- c# - 用户控件 BringToFront() 不起作用
- python - 如何让损失改变?
- php - 如何使用 PHP 和 Swift 5 解析 JSON
- c# - 根据服务方法/Web API 请求验证 Blazor 中的字段
- c# - 在对第一列进行排序之前,ComboBox 项目不会显示
- android - 无法解析方法 createSpeechRecognizer()
- python - TCP模型,实现seq_number
- performance - 如何提高使用多个包含的 EF Core 查询的性能