我想将以下sql语句转换为等效的pandas表达式。
select
a1.country,
a1.platform,
a1.url_page as a1_url_page,
a2.url_page as a2_url_page,
a1.userid, a1.a1_min_time,
min(a2.dvce_created_tstamp) as a2_min_time
from(
select country, platform, url_page, userid,
min(dvce_created_tstamp) as a1_min_time
from pageviews
group by 1,2,3,4) as a1
left outer join pageviews as a2 on a1.userid=a2.userid
and a1.a1_min_time < a2.dvce_created_tstamp
and a2.url_page <> a1.url_page
group by 1,2,3,4,5,6
我知道熊猫的合并命令,但是在我们的案例中,我们有一个复合联接子句,其中还包括不等式。我还没有找到有关如何处理这种情况的文档。
当然,我可以考虑最后遍历数据帧,但是我认为这不是最有效的方法。
例如,我们可以添加一些示例输入数据
----------------------------------------------------------------
| country | platform | url_page | userid | dvce_created_tstamp |
|----------------------------------------------------------------
| gr | win | a | bar | 2019-01-01 00:00:00 |
| gr | win | b | bar | 2019-01-01 00:01:00 |
| gr | win | a | bar | 2019-01-01 00:02:00 |
| gr | win | a | foo | 2019-01-01 00:00:00 |
| gr | win | a | foo | 2019-01-01 01:00:00 |
来自sql的响应
(编辑:添加样本数据) 显然,我们错过了 null a2_url_page
的行