🎓 A1Learner
🔍
0 of 48 lessons completed

Julia Grouping and Summaries

Grouping and summaries collect rows that share a category value into one set. A summary then reports one number for each set, such as a count or a mean.

Counts show how many rows each group holds

Rows are counted by category. The groups print in sorted order.

struct Sale
 category::String
 amount::Int
end
function count_groups(rows)
 counts = Dict{String, Int}()
 for r in rows
  counts[r.category] = get(counts, r.category, 0) + 1
 end
 return counts
end
rows = [Sale("books", 10), Sale("toys", 30), Sale("books", 20)]
counts = count_groups(rows)
for k in sort(collect(keys(counts)))
 println(k, ": ", counts[k])
end

Means show the average amount in each group

Amounts are added per group, then divided by the group count. The mean prints as a decimal number.

struct Sale
 category::String
 amount::Int
end
function mean_groups(rows)
 sums = Dict{String, Int}()
 counts = Dict{String, Int}()
 for r in rows
  sums[r.category] = get(sums, r.category, 0) + r.amount
  counts[r.category] = get(counts, r.category, 0) + 1
 end
 return sums, counts
end
rows = [Sale("books", 10), Sale("books", 20), Sale("toys", 30)]
sums, counts = mean_groups(rows)
for k in sort(collect(keys(sums)))
 println(k, ": ", sums[k] / counts[k])
end

Totals show how much each group holds

Amounts are added per group. The total prints for each group in sorted order.

struct Sale
 category::String
 amount::Int
end
function total_groups(rows)
 totals = Dict{String, Int}()
 for r in rows
  totals[r.category] = get(totals, r.category, 0) + r.amount
 end
 return totals
end
rows = [Sale("books", 5), Sale("books", 7), Sale("toys", 4)]
totals = total_groups(rows)
for k in sort(collect(keys(totals)))
 println(k, ": ", totals[k])
end

Example

struct Sale
 category::String
 amount::Int
end
function summarize(rows)
 counts = Dict{String, Int}()
 sums = Dict{String, Int}()
 for r in rows
  counts[r.category] = get(counts, r.category, 0) + 1
  sums[r.category] = get(sums, r.category, 0) + r.amount
 end
 return counts, sums
end
rows = [Sale("fruit", 10), Sale("fruit", 20), Sale("veg", 30), Sale("veg", 10)]
counts, sums = summarize(rows)
for k in sort(collect(keys(counts)))
 println(k, ": ", counts[k], " rows, mean ", sums[k] / counts[k])
end

Key points

  • Group by: collect rows that share one category value into one set.
  • Use a Dict to hold one entry for each category value.
  • Use sorted keys to print groups in a fixed order.
  • Use length to count the rows in a group.
  • Use sum divided by length to find the mean of a group.

In this example, rows are grouped by category, each group count is found, each group mean is found from its total divided by its count, and the groups print in sorted order.

Note

A mean is a summary of a group, not a row in the group.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

A list of sales has categories books, toys, and books. How should the groups print so the order never changes?