Four beaches, or twelve, depending who you ask
Field data with three faults in it, cleaned with Squirrel, whose method names are the sentences you would say out loud. The beach that collected the least starts out top of the table, and only one of the three faults fixes itself.
Four beaches, or twelve, depending who you ask
Squirrel is a data-wrangling library for .NET whose selling point is that the
method names are the sentences you would say out loud. RemoveIncompleteRows.
ExtractOutliers. Gist. The package is TableAPI and the namespace is
Squirrel.
The data is a season of volunteer beach-cleanup records, typed into a phone at the end of a cold morning, and it has the faults you would expect of that. This notebook is about how much those faults change the answer, which turns out to be completely: the beach that collected the least ends up top of the table.
Each cell below starts again from the file, so every pipeline reads top to bottom on its own and you can run any one of them without the others.
1#r "nuget: TableAPI, 1.0.3"2#r "nuget: ScottPlot, 5.1.59"
- TableAPI, 1.0.3
- ScottPlot, 5.1.59
Transitive dependencies (35)
- Antlr4, 4.6.4
- Antlr4.CodeGenerator, 4.6.4
- Antlr4.Runtime, 4.6.4
- HarfBuzzSharp, 8.3.1.1
- HarfBuzzSharp.NativeAssets.Linux, 8.3.1.1
- HarfBuzzSharp.NativeAssets.macOS, 8.3.1.1
- HarfBuzzSharp.NativeAssets.Win32, 8.3.1.1
- Microsoft.CSharp, 4.4.1
- NCalc2, 2.1.0
- Nunit, 3.13.2
- SkiaSharp, 3.119.0
- SkiaSharp.HarfBuzz, 3.119.0
- SkiaSharp.NativeAssets.Linux.NoDependencies, 3.119.0
- SkiaSharp.NativeAssets.macOS, 3.119.0
- SkiaSharp.NativeAssets.Win32, 3.119.0
- System.Collections, 4.3.0
- System.Diagnostics.Debug, 4.3.0
- System.Dynamic.Runtime, 4.3.0
- System.Globalization, 4.3.0
- System.IO, 4.3.0
- System.Linq, 4.3.0
- System.Linq.Expressions, 4.3.0
- System.ObjectModel, 4.3.0
- System.Reflection, 4.3.0
- System.Reflection.Emit, 4.3.0
- System.Reflection.Emit.ILGeneration, 4.3.0
- System.Reflection.Emit.Lightweight, 4.3.0
- System.Reflection.Extensions, 4.3.0
- System.Reflection.Primitives, 4.3.0
- System.Reflection.TypeExtensions, 4.3.0
- System.Resources.ResourceManager, 4.3.0
- System.Runtime, 4.3.0
- System.Runtime.Extensions, 4.3.0
- System.Threading, 4.3.0
- System.Threading.Tasks, 4.3.0
What arrived
DataAcquisition.LoadCsv returns a Table. Every cell is a string, which is the
honest representation of a CSV and the reason the cleaning below is necessary
rather than decorative.
1using System.Data;2using Squirrel;3using Squirrel.Cleansing;45// A Table renders as a table once its rows are projected into a DataTable.6DataTable Preview(Table table, int rows = 6)7{8 var headers = table.ColumnHeaders.ToArray();9 var output = new DataTable();1011 foreach (var header in headers)12 output.Columns.Add(header, typeof(string));1314 foreach (var row in Enumerable.Range(0, Math.Min(rows, table.RowCount)))15 output.Rows.Add(headers.Select(header => table[row][header]).ToArray());1617 return output;18}1920Preview(DataAcquisition.LoadCsv("cleanup-logs.csv"))
| Site | Volunteer | Date | Bags | Kilograms |
|---|---|---|---|---|
| cobble bay | Ada Lovelace | 2026-03-28 | 2 | 7.6 |
| Fern Point | Katherine Johnson | 2026-03-21 | 5 | 16.6 |
| Gull Reach | Ada Lovelace | 2026-05-16 | 2 | 5.1 |
| Gull Reach | Mary Jackson | 2026-05-30 | 2 | 5.9 |
| Gull Reach | Edsger Dijkstra | 2026-06-06 | 2 | 6.0 |
| Gull Reach | Alan Turing | 2026-08-15 | 1 | 2.9 |
Three faults, and what each one costs
Look closely at the Site column above. Whoever was holding the phone did not
always have the same case switched on, and that costs you the grouping: there
are four beaches, and a count of distinct values finds a dozen.
Gist is Squirrel's one-line summary of a table, and it is quietly making the
second point. It reports on Bags and says nothing at all about Kilograms,
because nine sessions were logged without a weight, and a column with holes in
it is not a number column.
1var logs = DataAcquisition.LoadCsv("cleanup-logs.csv");23Console.WriteLine($"rows {logs.RowCount}");4Console.WriteLine($"distinct Site values {logs.ValuesOf("Site").Distinct().Count()}");5Console.WriteLine($"weights left blank {logs.ValuesOf("Kilograms").Count(string.IsNullOrWhiteSpace)}");6Console.WriteLine();7Console.WriteLine("Gist of the file as it arrived:");89foreach (var line in logs.Gist())10 Console.WriteLine($" {line}");
rows 334
distinct Site values 12
weights left blank 9
Gist of the file as it arrived:
(Bags, Max, 7)
(Bags, Min, 1)
(Bags, Average, 2.8353293413173652)
(Bags, Range, 6)
Two of them fix themselves
Transform runs a function over one column, so the casing is a single line.
RemoveIncompleteRows drops any row with a blank in it. Run Gist again and
Kilograms has appeared, because the column is now numeric all the way down.
1var tidy = DataAcquisition.LoadCsv("cleanup-logs.csv")2 .Transform("Site", value => System.Globalization.CultureInfo3 .InvariantCulture.TextInfo.ToTitleCase(value.ToLowerInvariant()))4 .RemoveIncompleteRows();56Console.WriteLine($"rows {tidy.RowCount}");7Console.WriteLine($"distinct Site values {tidy.ValuesOf("Site").Distinct().Count()}");8Console.WriteLine();9Console.WriteLine("Gist once the holes are gone:");1011foreach (var line in tidy.Gist())12 Console.WriteLine($" {line}");
rows 325
distinct Site values 4
Gist once the holes are gone:
(Bags, Max, 7)
(Bags, Min, 1)
(Bags, Average, 2.806153846153846)
(Bags, Range, 6)
(Kilograms, Max, 7700)
(Kilograms, Min, 1.2)
(Kilograms, Average, 57.65846153846158)
(Kilograms, Range, 7698.8)
Look at that maximum. Somebody carried 7,700 kilograms off a beach.
The third fault does not fix itself
ExtractOutliers gives you the suspect rows rather than removing them, which is
the right way round: you want to see what a rule catches before you let it near
your data.
Z-score finds the two impossible entries. It also flags two sessions of 26 and 27 kilograms, which were simply good mornings. That is the usual failure of a statistical test on a column with extreme values in it: the two typos drag the mean and the standard deviation so far that ordinary rows start to look odd.
1var suspects = DataAcquisition.LoadCsv("cleanup-logs.csv")2 .RemoveIncompleteRows()3 .ExtractOutliers("Kilograms", OutlierDetectionAlgorithm.Z_Score);45foreach (var row in Enumerable.Range(0, suspects.RowCount))6 Console.WriteLine(7 $"{suspects[row]["Site"],-12} {suspects[row]["Date"]} " +8 $"{suspects[row]["Bags"],2} bags {suspects[row]["Kilograms"],8} kg");
Gull Reach 2026-08-08 2 bags 7300.0 kg Gull Reach 2026-04-04 1 bags 7700.0 kg Long Sands 2026-04-11 7 bags 27.5 kg Long Sands 2026-04-18 7 bags 26.2 kg
So do not use the statistic. Use the rule you would say out loud, which is that
nobody carries two hundred kilograms of litter off a beach in a morning.
Filter takes a predicate over the row and keeps what satisfies it.
1var cleaned = DataAcquisition.LoadCsv("cleanup-logs.csv")2 .RemoveIncompleteRows()3 .Filter(row => double.Parse(row["Kilograms"]) <= 200);45$"{cleaned.RowCount} sessions kept, two dropped"
323 sessions kept, two dropped
The answer, before and after
Pick narrows to the columns that matter and Aggregate sums by the first of
them. Run it once on the file as it arrived and once on the cleaned table, and
the league table is not the same league table.
1var titleCase = System.Globalization.CultureInfo.InvariantCulture.TextInfo;23var asArrived = DataAcquisition.LoadCsv("cleanup-logs.csv")4 .Transform("Site", value => titleCase.ToTitleCase(value.ToLowerInvariant()))5 .RemoveIncompleteRows()6 .Pick("Site", "Kilograms")7 .Aggregate("Site", AggregationMethod.Sum);89var asCleaned = DataAcquisition.LoadCsv("cleanup-logs.csv")10 .Transform("Site", value => titleCase.ToTitleCase(value.ToLowerInvariant()))11 .RemoveIncompleteRows()12 .Filter(row => double.Parse(row["Kilograms"]) <= 200)13 .Pick("Site", "Kilograms")14 .Aggregate("Site", AggregationMethod.Sum);1516double KilogramsAt(Table table, string site) =>17 Enumerable.Range(0, table.RowCount)18 .Where(row => table[row]["Site"] == site)19 .Select(row => double.Parse(table[row]["Kilograms"]))20 .First();2122var sites = Enumerable.Range(0, asCleaned.RowCount)23 .Select(row => asCleaned[row]["Site"])24 .OrderByDescending(site => KilogramsAt(asCleaned, site))25 .ToArray();2627var comparison = new DataTable();28comparison.Columns.Add("Site", typeof(string));29comparison.Columns.Add("As arrived (kg)", typeof(double));30comparison.Columns.Add("Cleaned (kg)", typeof(double));3132foreach (var site in sites)33 comparison.Rows.Add(34 site,35 Math.Round(KilogramsAt(asArrived, site), 1),36 Math.Round(KilogramsAt(asCleaned, site), 1));3738comparison
| Site | As arrived (kg) | Cleaned (kg) |
|---|---|---|
| Long Sands | 1297.9 | 1297.9 |
| Cobble Bay | 1006.8 | 1006.8 |
| Fern Point | 870.9 | 870.9 |
| Gull Reach | 15563.4 | 563.4 |
Gull Reach is last. Two stuck keys made it first by a factor of twenty-eight, and no amount of care further down the analysis would have caught it, because by then it is just a large number in a column of numbers.
1using ScottPlot;23var arrivedValues = sites.Select(site => KilogramsAt(asArrived, site)).ToArray();4var cleanedValues = sites.Select(site => KilogramsAt(asCleaned, site)).ToArray();56var plot = new Plot();7var bars = new List<Bar>();89for (var index = 0; index < sites.Length; index++)10{11 bars.Add(new Bar12 {13 Position = index - 0.21,14 Value = arrivedValues[index],15 Size = 0.38,16 FillColor = Colors.Gray.WithAlpha(0.55),17 Label = arrivedValues[index].ToString("N0"),18 });1920 bars.Add(new Bar21 {22 Position = index + 0.21,23 Value = cleanedValues[index],24 Size = 0.38,25 FillColor = Colors.Orange,26 Label = cleanedValues[index].ToString("N0"),27 });28}2930// One bar is twenty-eight times the others, so on a linear axis the three31// honest beaches flatten into each other. The value labels are how the real32// ranking stays readable without reaching for a log scale.33var drawn = plot.Add.Bars(bars);34drawn.ValueLabelStyle.IsVisible = true;35drawn.ValueLabelStyle.FontSize = 12;3637plot.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(38 Enumerable.Range(0, sites.Length).Select(index => (double)index).ToArray(),39 sites);4041plot.Title("Litter collected by beach: as it arrived (grey) and cleaned (orange)");42plot.YLabel("Kilograms");43plot.Axes.SetLimitsY(0, arrivedValues.Max() * 1.08);4445plot.ScaleFactor = 2;46plot.GetImageBytes(1880, 760, ImageFormat.Png)
Take it home
Download this notebook and cleanup-logs.csv from the gallery page, put them in
the same folder, then open it in VS Code with the Verso extension or serve it
with verso serve squirrel-cleanup.verso. Change the two-hundred-kilogram rule
to two thousand and rerun the last three cells: the typos come back, and so does
the wrong answer.
Run it yourself
Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.
dotnet tool install --global Verso.Cliverso serve squirrel-cleanup.versoThis notebook reads cleanup-logs.csv; download it into the same folder as the notebook.
What this shows
Field data with three faults in it, cleaned with Squirrel, whose method names are the sentences you would say out loud. The beach that collected the least starts out top of the table, and only one of the three faults fixes itself.
Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.