0

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'])

enter image description here

I've wrote some sql which will do the same thing but how to do it in 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

enter image description here

user3381431
  • 93
  • 2
  • 13

1 Answers1

1

You can do with 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')
BENY
  • 317,841
  • 20
  • 164
  • 234