Group by, percentage of total rows?

Apologies I’m very new to observable but I’m stumped and I’m sure its simple for many here.

Is there something similar to pandas group by, aggregate and apply a function to get counts and percentages of total rows?

eg group all rows by school_type, count and create a percentage of the total

df.groupby(‘dem_school_type_by_age_range’).agg({‘dem_school_type_by_age_range’: ‘count’}).apply(lambda x: 100*x/x.sum())

I’ve been looking for a while to figure out in vegalite, the data transform function… or do it inside of observables javascript but I can;t figure it out.

d3.rollup(…) is part of the standard library of Observablehq.

1 Like

Here’s a notebook showing how to use either Arquero or D3 for this. The Arquero code looks a lot like Pandas:


This is extremely helpful! Very clear. Thank you for taking the time to explain two alternative methods.

1 Like