如何总计获得不同的计数

How to get distinct count in aggregate

我只想获得distinct_count聚合。

我有这个代码:

data_frame = data_frame.group_by(:job_id)
                       .aggregate(job_id: :max, bid_id: :count)

我想要这样的东西:

data_frame = data_frame.group_by(:job_id)
                       .aggregate(job_id: :max, bid_id: :distinct_count)

我知道目前还没有实现这样的统计方法,还有其他方法吗?

我找到了一种方法:

data_frame = data_frame.group_by(:job_id)
                       .aggregate(job_id: :max,
                                  bid_id: lambda{ |x| x.uniq.size })

或者可能更好:

data_frame = data_frame.group_by(:job_id)
                       .aggregate(job_id: :max,
                                  bid_id: ->(x) { x.uniq.size })

我不确定这是否正确,但它似乎有效。

This pandas 解决方案帮助了我。