Multi-Column Data¶
A dict of arrays is a table with named columns. group_by() partitions the
rows by one or more key columns, and an aggregation then collapses each group
to a single row — the same GROUP BY you know from SQL, expressed in Python.
Group and sum¶
Group by one column, sum another:
sales = await create_object_from_value(
{
"category": ["Electronics", "Electronics", "Clothing", "Clothing", "Food"],
"amount": [500, 300, 150, 200, 80],
}
)
result = await sales.group_by("category").sum("amount")
data = await result.data()
print("Sales by category (sum):")
for cat, amt in sorted(zip(data["category"], data["amount"], strict=False)):
print(f" {cat}: ${amt}")
Multiple keys¶
Pass several columns to group by their combination:
orders = await create_object_from_value(
{
"region": ["East", "East", "West", "West", "East", "West"],
"category": ["A", "B", "A", "B", "A", "A"],
"revenue": [100, 200, 150, 250, 120, 180],
}
)
result = await orders.group_by("region", "category").sum("revenue")
data = await result.data()
print("Revenue by region + category:")
triples = sorted(zip(data["region"], data["category"], data["revenue"], strict=False))
for region, cat, rev in triples:
print(f" {region} / {cat}: ${rev}")
Several aggregations at once¶
agg() takes a mapping of column to aggregation, so one pass can compute
different reductions for different columns:
products = await create_object_from_value(
{
"category": ["Electronics", "Electronics", "Clothing", "Clothing"],
"price": [999.99, 499.99, 59.99, 89.99],
"quantity": [10, 25, 100, 75],
}
)
result = await products.group_by("category").agg(
{
"price": "mean",
"quantity": "sum",
}
)
data = await result.data()
print("Product stats by category:")
for i, cat in enumerate(data["category"]):
print(f" {cat}: avg price=${data['price'][i]:.2f}, total qty={data['quantity'][i]}")
Next¶
Views & Filters → — slice a table with WHERE, ORDER BY, and LIMIT.
See Also¶
- Object API —
group_by,agg, andhaving - Examples: Group By — every grouping pattern, including HAVING
- Examples: Aggregation Table — wide aggregation output