熊猫groupby数量和占总数的比例

时间:2018-12-11 02:41:34

标签: python sql pandas

我正在尝试对熊猫做以下事情。然后按州计算项目,然后将该数字表示为小计的百分比。我的数据框包含原始数据。我可以得到计数,但是如何在百分比后面添加另一列?

state_grp = df.groupby(by=['date', 'state','ad_type'])
state_grp.ad_type.agg(['count'])

enter image description here

我写了一些sql,它会做同样的事情,但是如何在熊猫中做呢?

with cte1 as
(
    select distinct date, state, ad_type, count(ad_type) over (partition by date, state, ad_type) as [# of Ads]
    from propertylistings
),

cte2 as
(
    select *, sum([# of Ads]) over (partition by state) as subtotal
    from dhg
)

select date, state, ad_type, [# of Ads], round(cast([# of Ads] as float)/cast(subtotal as float) * 100, 1) as [%]
from cte2
order by date, state, ad_type

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用transform + sum

state_grp = df.groupby(by=['date', 'state','ad_type'])
state_grp=state_grp.ad_type.agg(['count'])
state_grp['%']=state_grp['count']/state_grp.groupby(level=[0,1])['count'].transform('sum')