Data Wrangling

The Data Science unit on the work between receiving data and analyzing it, done on real public files. Exploring with groups, contingency and pivot tables, with a reversal explained by age composition; tidy data, reshaping, types, dates and scaling; special codes, a file-format zero that pandas misreads, suppressed counts and the three missing-data mechanisms with an experiment on the common remedies; joins checked with validate and indicator, a death-rate join checked against the published national figure, and stacked survey cycles; and the formats, pipelines, warehouses and hashes of data engineering. Library behavior checked under pandas 2.2.3 and 3.1.1 in November 2026.

Raw data are rarely ready for analysis. Data wrangling is the work that comes first: finding out what a file contains, putting it into a shape that analysis can use, repairing what is wrong or missing, combining it with other sources and saving the result so that it can be checked and rerun. None of it shows in the final numbers, yet every one of them depends on it. This unit takes the steps one at a time, each in an article that makes the decisions explicit and measures what they change.

The examples are real public files with real problems. The main running data are the 2017 to 2018 National Health and Nutrition Examination Survey (NHANES) files on demographics, blood pressure, body measurements and weight history, whose numbers include refusal codes, readings recorded as zero and a file format whose zeros a common reader loses. Beside them are the Census Bureau's population estimates, which store totals in the same table as their parts, and the National Center for Health Statistics weekly death counts by state, with suppressed cells and jurisdictions that overlap. Every number comes from a script in tools/experiments/data-science/ that downloads the public files and checks them by hash. Library behavior changes between versions, so the scripts were rerun on 23 November 2026 under Python 3.13.5 with pandas 2.2.3 and NumPy 2.2.5, and again with pandas 3.1.1, the current release on that date, and NumPy 2.5.4 (NumPy 2.5.5, released on 15 November 2026, was current); where the two differ, the articles give both results.

Articles, in order

  • Introduction to Data Wrangling: what wrangling is and how much of an analysis it is thought to take; the steps of exploring, structuring, cleaning, enriching, validating and publishing; wrangling against extract-transform-load pipelines; and one complete wrangle from two raw NHANES files to mean blood pressure by age, with the rows counted at every step. Its worked problems apply the survey weights and show why the mean of person means differs from the mean of all readings.
  • Exploring Data with Groups and Pivot Tables: split-apply-combine and its pandas form; size against count and missing group keys; contingency tables read by rows and by columns; pivot tables and their margins; and a reversal between adults with and without a usual place of care, explained by age composition and removed by direct standardization. The worked problems split the crude difference exactly, prove that no standard can restore the crude ordering and test each age-specific gap.
  • Structuring Data: tidy data and the common ways files break it; wide and long layouts; headers that hold values and rows that hold totals; types, ordered codes and one-hot encoding; dates and epidemiological weeks; and rescaling, with the effect of units on nearest neighbors measured. The worked problems test the fall from the first reading to the second, prove that the average squared distance over pairs is twice the variance and show a scaler meeting data outside its training range.
  • Cleaning Data: special codes, structural against unknown missing values, the transport-file zero traced to the conversion formula, units and self-reports, totals beside their parts, suppressed counts and duplicates; then Rubin's three missing-data mechanisms and a seeded experiment on four remedies. The worked problems derive what mean imputation does to the spread and the slope, prove why complete cases keep the slope under missingness that depends on age only, test the real missing readings and recover the suppressed total from the national row.
  • Enriching Data: joins, what each type keeps and the checks that catch a wrong one; a join that turns 2,808 rows into 12 million; state death rates from two agencies' files, checked against the published national rate; stacking two survey cycles whose columns changed; and derived features with the bin-edge trap. The worked problems predict the size of a join before running it, account for the gap to the published death count and reconcile two sets of age bins.
  • Data Engineering: file formats and what each keeps of a table, with row and column layouts compared on a real file; schemas and checks; extract-transform-load and extract-load-transform run through SQLite; databases, warehouses with star schemas, lakes and lakehouses; MapReduce, HDFS and Spark; and reproducibility by pinning inputs with hashes. The worked problems give the expected change in a digest, add a combiner to the MapReduce example and separate the bytes of a file from the content of a table.

The order follows the steps of a wrangle. The first article runs through all of them once on a small question, and the next four take one step each, in the order an analysis usually meets them. The last steps back from the single analysis to the systems that store and move data for many analyses. Each article ends with open questions that an experiment or a source could settle.

Where the site goes deeper

Several subjects these articles touch are treated in full elsewhere. The pandas Package introduces the library itself. The Statistics collection proves what the wrangling articles use: Survey Weights and the Horvitz–Thompson Estimator for the weights that turn the NHANES adults into population estimates, Confounding and Causal Diagrams for the reversal read as confounding by age, Direct Standardization for standardized rates and their variance, The Chi-Square Test of Independence for the test of missingness, and The EM Algorithm for maximum likelihood with incomplete data, the principled alternative to imputation under missingness at random. In the Information Retrieval collection, Near-Duplicate Detection works through the problem of deciding when two records describe the same thing, and Index Construction puts MapReduce to work on a large job. The relational side of joins and storage is in Join Queries and Relational Databases.

Some of the statistics of preparing data have no home on the site yet. They include methods for missing data beyond those measured here (multiple imputation with its combining rules, chained equations and sensitivity analysis for data missing not at random), probabilistic record linkage, feature engineering and its encodings, data leakage, and the validation and versioning of datasets.

References