datamash -t, --header-in -g 3 mean 4 < data.csv

Category: Data Science & Analysis

datamash -t, --header-in -g 3 mean 4 < data.csv

Compute mean of column 4 grouped by column 3

The -g 3 flag groups rows by the values in column 3 before applying the statistic, a group-by operation. Here each unique value in column 3 (say a department name) gets its own mean of column 4 (say salary). Output rows appear in first-seen order unless you add -s to sort the groups. This is the datamash equivalent of a SQL GROUP BY with AVG.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.