简体   繁体   English

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

[英]pandas groupby count and proportion of group total

I'm trying to do the following with pandas. 我正在尝试对熊猫做以下事情。 Counting item by state then expressing that number as a percentage of the subtotal. 然后按州计算项目,然后将该数字表示为小计的百分比。 My dataframe has the raw data. 我的数据框包含原始数据。 I can get the counts but how to append another column for the percentages? 我可以得到计数,但是如何在百分比后面添加另一列?

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

在此处输入图片说明

I've wrote some sql which will do the same thing but how to do it in pandas? 我写了一些sql,它会做同样的事情,但是如何在pandas中做呢?

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

在此处输入图片说明

You can do with transform + sum 你可以用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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM