Data Wrangling and Visualization

Chapter 8
Data Integration, Reshaping and Specialized Data

Diploma in Data science
Data Wrangling and Visualization   •   Diploma

Data Integration, Reshaping and Specialized Data

Introduction

Data rarely arrives in the form required for analysis. A retailer may store customers, orders, products, payments and delivery events in separate systems. A public-health analyst may combine demographic records, hospital visits and geographic boundaries. A business dashboard may require a single table even though the source data is distributed across databases, spreadsheets, APIs and text files.

Data integration is the disciplined process of combining data from multiple sources while preserving meaning, quality, traceability and analytical usefulness. Reshaping changes the structural arrangement of observations, commonly between wide and long formats. Specialized data requires additional preparation because time, language and geographic location have distinct properties that ordinary tabular methods do not fully capture.

This chapter develops practical foundations for joining, merging, appending, pivoting and reconciling datasets. It also explains keys, entity matching, granularity, data lineage, temporal data, text data and geospatial data. The emphasis is on reliable workflows rather than merely producing a technically successful output.

Learning Objectives

By the end of this chapter, you should be able to:

  • distinguish joins, merges, appends and unions;
  • select appropriate keys and diagnose key-quality problems;
  • perform and validate one-to-one, one-to-many and many-to-many integration;
  • use entity matching when identifiers are absent or unreliable;
  • reshape data between wide and long forms using pivoting and unpivoting;
  • reconcile conflicting records and preserve data lineage;
  • prepare time-series, text and geospatial data for analysis;
  • control granularity and avoid duplication, leakage and incorrect aggregation;
  • evaluate ethical, governance and operational risks in integrated datasets.

1. The Logic of Data Integration

A dataset consists not only of values but also of observations, entities, attributes, time references and definitions. Integration is valid only when these dimensions are understood.

An entity is the object being described, such as a customer, product, hospital or geographic region. An observation is a recorded fact about an entity, such as an order or a daily temperature. An attribute is a property such as customer type or product price. A key is an attribute, or combination of attributes, used to identify or relate records.

A useful conceptual model is:

Data integration workflow Source systemsfiles, APIs, databases Standardisetypes, names, units Integratekeys and relationships Validatecounts and quality Analytical datasetdocumented grain • reproducible lineage • known limitationsfit for a stated analytical purpose
Data integration workflow from source systems to validated analytical dataset
Figure: Data integration workflow from source systems to validated analytical dataset

Figure: A controlled workflow for producing an integrated analytical dataset.

Integration should begin with the analytical question. For example, “monthly revenue by region” requires a definition of revenue, a region assignment, a reporting period and a decision about refunds and cancelled orders. Without these definitions, technically correct operations can produce an invalid result.

Common integration operations

A join combines columns from related tables using a matching condition. A merge is often used as a synonym, although some software uses it for a specific join operation. An append stacks rows from datasets with compatible columns. In relational terminology, appending is closely related to a union.

OperationMain effectTypical use
Inner joinKeeps matching records in both tablesAnalyse customers who placed orders
Left joinKeeps every record in the left tableAdd customer attributes to all orders
Right joinKeeps every record in the right tableLess common; equivalent to reversing a left join
Full outer joinKeeps records from both tablesAudit unmatched entities
Semi-joinKeeps left records having a match, without adding columnsFilter to eligible entities
Anti-joinKeeps left records without a matchFind missing reference records
Append/unionAdds rowsCombine monthly files with the same schema

An inner join can silently exclude records. A left join can preserve the population of interest but introduce missing values. A full join is useful for reconciliation because it reveals records existing in only one source.

2. Keys, Cardinality and Granularity

A primary key uniquely identifies each row in a table. A foreign key refers to a key in another table. A natural key has meaning in the domain, such as an official registration number. A surrogate key is an internally generated identifier. A composite key uses multiple fields, such as customer_id and order_date.

Before joining, identify the grain: the level represented by one row. Examples include “one row per customer,” “one row per order,” “one row per order item” and “one row per store per day.” A join is safe only when its cardinality is understood.

  • One-to-one: each key occurs at most once in each table.
  • One-to-many: one record in the first table relates to several records in the second.
  • Many-to-one: several records in the first table relate to one reference record.
  • Many-to-many: several records on both sides relate to each other.

Many-to-many joins can multiply rows. If customer C appears twice in a customer table and three times in an order table, a direct join may produce six combinations. This is not a software error; it reflects ambiguous relationships. Usually, one side should be deduplicated, or a bridge table should explicitly represent the relationship.

A key-quality audit should examine:

  1. uniqueness;
  2. missingness;
  3. type and format consistency;
  4. leading zeroes and whitespace;
  5. case sensitivity;
  6. referential integrity;
  7. validity over time;
  8. whether the key identifies the intended entity rather than merely a transaction.

For a table expected to have a unique key, the duplicate rate can be expressed as:

Duplicate rate = (number of rows with non-unique key values) ÷ (total number of rows)

The formula is a descriptive quality measure. The numerator must be defined consistently—for example, counting all rows belonging to duplicated key groups rather than counting only extra rows. It should not be interpreted as a universal data-quality standard.

Worked example: preventing row multiplication

Suppose an order table contains 1,000 rows and an order-item table contains 2,400 rows. Joining orders to items is expected to produce approximately 2,400 rows because the grain becomes one row per item. If a customer table contains one row per customer, adding it to the item-level result should preserve 2,400 rows, provided customer_id is unique in the customer table. If the result contains 2,470 rows, the customer table may contain duplicated identifiers or the join condition may be incomplete.

A professional workflow records row counts before and after every integration step and explains expected changes.

3. Joining, Merging and Appending in Practice

A reliable join workflow is:

  1. state the grain of both inputs;
  2. inspect key uniqueness and missingness;
  3. standardise key representation;
  4. choose the join type according to the business question;
  5. perform the join;
  6. measure matched and unmatched records;
  7. test row counts and important totals;
  8. document assumptions and output lineage.

Key standardisation may involve trimming whitespace, converting text case, normalising date formats and removing formatting characters. However, transformations must not destroy meaningful distinctions. For example, removing leading zeroes from an account code can create false matches.

Appending requires compatible meanings, not merely compatible column names. A column called amount may mean gross sales in one file and net sales in another. Before appending, align names, data types, units, category codes and time coverage. Missing columns should be introduced explicitly and documented rather than silently inferred.

A union of datasets with identical schemas should not be confused with deduplication. If the same record occurs in two monthly extracts, appending creates duplicate observations. Deduplication requires a rule, such as retaining the latest trusted version according to source priority and update time.

Reconciliation

Reconciliation compares records or aggregates from different sources. For a numeric measure, an absolute difference is:

Δ = A − B

where A and B are comparable values measured in the same units. A relative difference, when B ≠ 0, is:

Relative difference = (A − B) ÷ |B|

These measures are meaningful only when definitions, periods, currencies, filters and rounding conventions agree. A difference does not automatically indicate an error; it may reflect timing, scope or legitimate business rules.

A reconciliation table should include source values, difference, match status, reason code and responsible owner. Common reason codes include missing record, duplicate record, late-arriving transaction, currency conversion, changed classification and rounding.

4. Entity Resolution and Fuzzy Matching

Entity resolution identifies records that refer to the same real-world entity despite inconsistent identifiers. For example, “A. Rahman Ltd,” “A Rahman Limited” and “A-Rahman Ltd.” may represent one organisation.

The strongest approach is deterministic matching using a trusted identifier. When this is unavailable, matching may use normalised names, addresses, telephone numbers, email domains or combinations of fields. Fuzzy matching compares similarity rather than exact equality, but it should support human-reviewed decisions rather than operate as an unquestioned truth machine.

A typical process is:

  • profile fields and determine their reliability;
  • standardise punctuation, casing, abbreviations and formats;
  • create candidate pairs using blocking, such as the same postcode or email domain;
  • calculate comparison features;
  • assign match, non-match or review outcomes;
  • retain evidence and confidence;
  • evaluate false positives and false negatives.

A simple string similarity score may be represented generically as:

S = ∑ wᵢsᵢ ÷ ∑ wᵢ

where sᵢ is the similarity for field i, wᵢ is its chosen weight, and the denominator is positive. The score is a model-dependent ranking aid, not a universal probability. Weights and thresholds must be validated against labelled examples. A high name similarity should not override a conflicting government identifier.

Entity matching creates ethical risks. Incorrectly merging two people can expose private information or distort eligibility decisions. Incorrectly splitting one person can fragment service histories. Sensitive applications require conservative thresholds, review procedures, access controls and an appeal mechanism.

5. Reshaping Data: Wide and Long Forms

In wide format, repeated categories or periods appear as columns. In long format, each row represents one observation identified by variables such as entity, time and measure.

Example wide data:

productJanFebMar
A101215
B8911

Long form:

productmonthsales
AJan10
AFeb12
AMar15
BJan8
BFeb9
BMar11

Pivoting commonly converts long data to wide data by using an index, columns and values. Unpivoting or melting converts repeated columns into key-value rows. Long form is generally convenient for grouping, filtering, visualisation and statistical modelling. Wide form can be convenient for reports, matrix operations and some spreadsheet workflows.

A pivot requires that each combination of index and pivot-column value identifies at most one value. If multiple values exist, an aggregation rule such as sum, mean, minimum or maximum is needed. The chosen aggregation must match the meaning of the data. Summing prices is normally invalid, whereas summing quantities may be appropriate.

A common reshaping error is losing the measure name. If sales, returns and profit are all placed into one value column, retain a variable such as measure so that the values remain distinguishable. Also preserve units and missing-value semantics: zero means an observed absence or quantity of zero, while missing may mean unknown or not reported.

6. Time-Series Data

Time-series data is indexed by time and often has ordering, frequency, duration and time-zone properties. Important distinctions include:

  • instant: a point such as 09:30:00;
  • interval: a period such as January;
  • duration: elapsed time between two instants;
  • frequency: regularity such as hourly, daily or monthly;
  • event time: when an event occurred;
  • processing time: when the system recorded or processed it.

Dates should be parsed with an explicit format where possible. Time zones must be retained when observations refer to global events. Converting every timestamp to local time without recording the original zone can produce incorrect ordering and duplicate or missing hours during daylight-saving transitions.

Resampling changes frequency. Aggregating daily sales to monthly sales is valid when the measure is additive and the time boundaries are defined. For a set of transaction values x₁, …, xₙ in a period, the period total is:

T = ∑ᵢ₌₁ⁿ xᵢ

This assumes each transaction is included once, values share a unit and the measure is additive. An average daily temperature should not be computed by summing temperatures; a weighted or appropriately defined mean may be required.

Time-series integration must prevent temporal leakage. A feature used to predict an outcome at time t must be constructed only from information available at or before the prediction time. Joining a later correction or future outcome onto an earlier record can produce unrealistically strong model performance.

Important checks include duplicate timestamps, gaps, overlapping intervals, inconsistent frequency, late-arriving events, clock changes and invalid future dates. A date dimension can help standardise reporting periods, fiscal calendars, holidays and week definitions.

7. Text Data Preparation

Text data is semi-structured and often contains spelling variation, punctuation, abbreviations, multiple languages and sensitive content. Preparation begins by preserving the original text and creating derived versions for analysis.

Common operations include Unicode normalisation, whitespace handling, case transformation, tokenisation, stop-word treatment, stemming or lemmatisation, language detection and removal or masking of personally identifiable information. No single operation is universally correct. Case may matter in product codes, stop words may carry meaning in legal text, and stemming can reduce interpretability.

Text integration often requires mapping free-text categories to controlled vocabularies. For example, “card,” “credit card” and “visa card” may or may not represent the same business category. The mapping should be versioned and reviewed.

Duplicate detection can use exact hashes, normalised text or similarity methods. Near-duplicate documents should not be counted as independent evidence without investigation. Text-derived features also inherit bias from language, source population and annotation practices.

Natural-language models can assist classification, extraction and entity recognition, but outputs are probabilistic and context-dependent. AI-generated labels should be sampled for quality, monitored across demographic and linguistic groups, and linked to the model version and prompt or configuration used. Sensitive text should not be sent to external services without appropriate authorisation and contractual safeguards.

8. Geospatial Data

Geospatial data describes locations, shapes and relationships in space. Common geometries are points, lines and polygons. A point may represent a store, a line a road and a polygon a district.

A coordinate reference system (CRS) defines how locations are represented. Latitude and longitude are angular coordinates, commonly expressed in degrees. Projected coordinate systems represent space on a plane and use linear units such as metres. Distance and area calculations should generally use an appropriate projected CRS or a geodesic method; treating degrees as metres produces invalid measurements.

Typical operations include:

  • spatial join: attach attributes according to spatial relationship;
  • point-in-polygon: assign an event to a region;
  • buffer: create a zone around a feature;
  • intersection: find overlapping geometries;
  • nearest-neighbour matching: associate a feature with the closest candidate;
  • dissolve: combine boundaries by a shared category.

A spatial join can multiply records when a point lies in overlapping polygons. Boundary definitions may also change over time, so historical observations should be joined to the boundary version valid for the relevant period where possible. Coordinate precision can create privacy risks: publishing exact locations of vulnerable individuals may enable re-identification.

Geospatial analysis also faces the modifiable areal unit problem: results can change when boundaries or aggregation levels change. Aggregated regional patterns should not automatically be interpreted as individual behaviour. This is an ecological inference error.

9. Data Lineage, Provenance and Governance

Data lineage records where data came from, what transformations were applied, when they occurred and which output fields depend on which inputs. Provenance includes the broader history and context of data creation and use.

A useful lineage record contains source name and version, extraction time, transformation logic, software environment, quality checks, responsible person or system, schema version and known limitations. Reproducible pipelines should be idempotent where practical: rerunning the same inputs and specified code should produce the same output, apart from documented nondeterminism.

Governance should address ownership, access, retention, correction, security, consent and permitted use. Integration can increase the sensitivity of data even when individual sources appear harmless. For example, combining location, purchasing and demographic data may enable detailed profiling.

Apply data minimisation: integrate only fields necessary for the stated purpose. Use role-based access, encryption, audit logs, retention limits and controlled exports. Where law or institutional policy requires it, conduct a privacy impact assessment and document the lawful or authorised basis for processing.

10. Illustrative Case: Integrating Retail Operations Data

Background

A fictional regional retailer wants to identify monthly sales by store and customer segment. Its systems contain point-of-sale transactions, a customer relationship database, a store directory and a monthly product catalogue.

Problem

The transaction system uses customer_no, the customer system uses customer_id, and some transactions have no customer identifier. Store codes contain leading zeroes in one source. The product catalogue contains monthly prices, while transactions contain actual sale prices. A preliminary join reports sales 18% higher than the finance ledger.

Analysis

The team first states the intended grain: one row per transaction line for operational analysis and one row per store-month-segment for reporting. They discover that the customer table contains two records for several customers because of historical migrations. Directly joining it would duplicate transaction lines. The product catalogue also contains multiple records per product because prices vary by effective date.

Approach and solution

The team creates a customer crosswalk linking legacy and current identifiers, with an effective date and review status. Only one valid customer record is selected for each transaction at its transaction date. Store codes are standardised without removing meaningful leading zeroes. Product prices are joined using product code plus an effective-date rule, but actual transaction price remains the source for revenue.

Transactions are checked for duplicate line identifiers, cancelled sales and refunds. Customer information is added with a left join so anonymous transactions remain in the sales population. Monthly aggregation is performed only after the transaction-level checks. The report includes a separate “anonymous customer” category rather than treating missing identity as a known segment.

A reconciliation with finance shows that the original 18% difference came from joining duplicate customer records and including cancelled transactions. After correction, the remaining difference is explained by the finance ledger’s month-end posting rule. The reconciliation report records this rule rather than forcing the two systems to match.

Outcome

The retailer obtains a reproducible dataset with documented grain, source priorities, unmatched-record counts, effective dates and known limitations. Analysts can distinguish actual sales from catalogue prices and identifiable from anonymous transactions.

Learning points

  • define grain before joining;
  • use effective dates for changing reference data;
  • preserve records when missingness is meaningful;
  • reconcile definitions, not only numbers;
  • retain crosswalks and evidence for entity matching;
  • aggregate only after duplication and validity checks.

11. Common Failure Modes and Best Practices

Frequent failures include joining on names without standardisation, assuming a key is unique, silently coercing data types, appending incompatible definitions, aggregating before resolving duplicates, confusing missing with zero, ignoring time zones, using latitude and longitude as planar distances, and discarding source fields needed for audit.

Best practices are:

  • create a data contract describing fields, types, grain and ownership;
  • profile every source before transformation;
  • use explicit join validation where the software supports it;
  • retain unmatched records and investigate them;
  • write assertions for expected row counts and uniqueness;
  • use stable, version-controlled reference tables;
  • preserve raw data separately from curated data;
  • record transformation and model versions;
  • test representative edge cases, including nulls, duplicates and boundary dates;
  • obtain domain review for ambiguous matches and classifications;
  • communicate uncertainty and limitations with the result.

Contemporary data platforms increasingly support schema registries, data-quality observability, lineage graphs, streaming integration and machine-assisted entity resolution. These tools improve scale and monitoring, but they do not replace semantic decisions. Automation should make assumptions visible, not conceal them.

Chapter Summary

Data integration combines information from multiple sources while maintaining meaning, quality and traceability. Joins add columns according to keys and relationships; appends add rows when schemas and meanings are compatible. Join cardinality and data grain must be understood because non-unique keys can multiply records and distort measures.

Entity resolution links records representing the same real-world entity. Deterministic identifiers are preferable, while fuzzy matching requires thresholds, validation, review and evidence. Reshaping converts between long and wide forms; pivot operations require an appropriate aggregation rule when combinations are not unique.

Time-series preparation involves timestamps, time zones, intervals, frequency, resampling and protection against temporal leakage. Text preparation must respect language, context, privacy and model uncertainty. Geospatial preparation requires valid coordinate reference systems, suitable distance methods and awareness of boundary effects and ecological inference.

Reliable integration includes reconciliation, data lineage, provenance, governance, privacy protection and reproducible validation. The final dataset should be judged not only by whether code runs, but by whether its grain, definitions, assumptions and limitations are fit for the intended decision.

Key Terms

Append: Add rows from one dataset to another.

Cardinality: The relationship pattern between matching records, such as one-to-many.

Composite key: A key formed from two or more fields.

Data grain: The level represented by one row.

Data lineage: A record of sources and transformations leading to an output.

Entity resolution: Identifying records that refer to the same real-world entity.

Foreign key: A field referring to a key in another table.

Full outer join: A join retaining matched and unmatched records from both inputs.

Inner join: A join retaining only records with matches in both inputs.

Key: A field or fields used to identify or relate records.

Left join: A join retaining every record from the left input.

Long format: A structure in which repeated observations occupy rows.

Pivot: A transformation that reorganises values across rows and columns.

Primary key: A field or combination uniquely identifying rows.

Provenance: Context and history describing data creation and transformation.

Reconciliation: Systematic comparison of records or measures from different sources.

Spatial join: Joining records according to a geographic relationship.

Temporal leakage: Use of information unavailable at the time an analysis or prediction was supposed to occur.

Wide format: A structure in which repeated categories or periods occupy columns.

Review Questions

Short Answer Questions

  1. What is the difference between a join and an append?
  2. Why must data grain be identified before integration?
  3. Define a composite key and give one example.
  4. What is entity resolution?
  5. Why can latitude and longitude not generally be treated as distances in metres?

Long Answer Questions

  1. Explain the main join types and discuss when each is appropriate.
  2. Describe a complete workflow for validating a one-to-many join.
  3. Compare wide and long data formats, including their advantages and limitations.
  4. Explain the major preparation issues associated with time-series data.
  5. Discuss how lineage, privacy and governance affect integrated datasets.

Analytical / Application Questions

  1. A customer table contains duplicate customer identifiers, and an order table contains several orders per customer. Design a workflow to integrate them without duplicating orders. State the checks you would perform.
  2. A dataset contains store locations in latitude and longitude and district polygons in a projected CRS. Explain how you would conduct a reliable point-in-polygon analysis and validate the result.
  3. Two financial systems report different monthly revenue totals. Develop a reconciliation plan that distinguishes data errors from differences in timing, scope, currency or business definitions.

Multiple Choice Questions with Answers

Question 1

Which operation keeps every record from the left table and adds matching columns from the right table?

A. Inner join

B. Left join

C. Full outer join

D. Append

Correct Answer: B. Left join

Question 2

What is the main risk of joining two tables when the join key is duplicated in both tables?

A. All missing values become zero

B. Rows may be multiplied through many-to-many matching

C. The tables are automatically sorted

D. The data types become numeric

Correct Answer: B. Rows may be multiplied through many-to-many matching

Question 3

Which structure normally places repeated observations in rows rather than separate period columns?

A. Wide format

B. Long format

C. Indexed format

D. Nested format

Correct Answer: B. Long format

Question 4

A transaction table has 2,000 rows and a unique customer table is left-joined to it. Assuming every transaction has at most one customer match, what should happen to the transaction row count?

A. It should remain 2,000

B. It should become 1,000

C. It should become the number of customers

D. It must become greater than 2,000

Correct Answer: A. It should remain 2,000

Question 5

Which practice best reduces temporal leakage in a predictive dataset?

A. Using all available data regardless of date

B. Using only information available by the prediction time

C. Replacing dates with row numbers

D. Sorting records alphabetically

Correct Answer: B. Using only information available by the prediction time

Question 6

Why is a projected coordinate reference system often preferred for local distance or area calculations?

A. It removes all missing coordinates

B. It represents measurements using suitable linear units

C. It guarantees accurate entity matching

D. It converts polygons into text

Correct Answer: B. It represents measurements using suitable linear units

Question 7

What is the primary purpose of an anti-join in data quality work?

A. To find left-table records without a match

B. To calculate an average

C. To convert long data to wide data

D. To encrypt identifiers

Correct Answer: A. To find left-table records without a match

Question 8

In fuzzy entity matching, what should a similarity score primarily be treated as?

A. An unquestionable legal identity

B. A ranking or decision aid requiring validation

C. A replacement for all source identifiers

D. A guarantee that two records are different

Correct Answer: B. A ranking or decision aid requiring validation

Question 9

Which statement about missing and zero is correct?

A. They always have identical meanings

B. Missing usually indicates unknown or unreported, whereas zero may be an observed quantity

C. Zero should always be converted to missing

D. Missing should always be converted to zero

Correct Answer: B. Missing usually indicates unknown or unreported, whereas zero may be an observed quantity

Question 10

What does data lineage document?

A. Only the final dashboard colours

B. The sources and transformations leading to an output

C. Only the number of rows in a table

D. The geographic distance between records

Correct Answer: B. The sources and transformations leading to an output

Data Wrangling and Visualization
Chapter 8 of 15