Data Wrangling: Turn Source Data into an Analysis-Ready Dataset
Data wrangling is the work of bringing source data into a structure that can answer a question, organizing and transforming it, combining sources, and checking the result. The goal is not to make every source look “clean” in the abstract. First define what one row in the target dataset should mean, then reshape the data as needed and verify that rows or values were not accidentally added or lost.
Data wrangling is the work of bringing source data into a structure that can answer a question, organizing and transforming it, combining sources, and checking the result. The goal is not to make every source look “clean” in the abstract. First define what one row in the target dataset should mean, then reshape the data as needed and verify that rows or values were not accidentally added or lost.
How is it different from data cleaning?
Data cleaning focuses on finding and handling quality problems such as entry errors, missing values, and inconsistent formats. Wrangling can include cleaning, but it is broader: reading files, standardizing column names and types, reshaping wide tables into long ones, joining other tables, deriving variables for analysis, and validating the output. In practice, tools and organizations use the terms in overlapping ways. Rahm and Do also describe cleaning alongside structural and representational transformations and the integration of multiple sources.
The first question is the row grain of the target table. The transformation and join results differ depending on whether each row should represent one store or one store-month combination. A tidy-data layout—one observation per row, one variable per column—is often useful for analysis and moving between tools, but a wide table may be more convenient for reporting or data entry. Long form is not always the right answer. Wickham’s paper on tidy data presents this structure as one convention for representing data.
Reshape a wide monthly table into analysis rows
The following is a hypothetical example for explaining the calculation. Assume sales are measured in thousands of Korean won.
| store_id | January sales (thousand KRW) | February sales (thousand KRW) |
|---|---|---|
| S-01 | 10 | 12 |
| S-02 | 7 | 9 |
This layout is convenient for data entry or a quick comparison. For analysis by store and month, it may be easier to use one row per store-month. melt produces four observations:
| store_id | month | sales_thousand_krw |
|---|---|---|
| S-01 | January | 10 |
| S-01 | February | 12 |
| S-02 | January | 7 |
| S-02 | February | 9 |
The values have not changed; only their representation has. Month moved from separate column names into a month value. In pandas, melt() keeps identifier columns and turns measured columns into variable/value rows. The reverse operation, pivot(), can reorganize rows and columns, but each chosen index-and-column combination must be unique. If a combination has multiple values, decide whether to aggregate them or redefine the source row grain first. The pandas reshaping guide describes wide and long formats, melt, and pivot.
Join a lookup table and check the row count
Suppose a separate lookup table maps each store to a region:
| store_id | region |
|---|---|
| S-01 | Seoul |
| S-02 | Busan |
The monthly sales table repeats each store ID, while the lookup should contain only one row per store. In pandas, validate="many_to_one" checks that the lookup-side key is unique before joining. With indicator=True, you can distinguish matched rows from keys found only on the left. A left-only row means a store has no region in the lookup; decide whether to update the lookup or analyze it as unmatched rather than silently ignoring or deleting it. pandas can match null join keys to each other, unlike usual SQL joins, so check missing keys instead of assuming identical behavior. See the pandas merge guide and pandas.merge reference.
This illustrative pandas example assumes sales_wide has the two sales columns shown above and store_lookup has one row per store ID.
sales_long = sales_wide.melt(
id_vars=["store_id"],
var_name="month",
value_name="sales_thousand_krw",
)
result = sales_long.merge(
store_lookup,
on="store_id",
how="left",
validate="many_to_one",
indicator=True,
)
unmatched = result.loc[result["_merge"] != "both"]If both stores have lookup records, the result should have four rows. Total sales are 10 + 12 + 7 + 9 = 38 thousand KRW; Seoul accounts for 22 thousand and Busan for 16 thousand. If the joined result has more than four rows, check whether store_id appears more than once in the lookup and multiplied sales rows. If it has fewer than four, check the join type, missing input rows, and differences in key formatting. These are illustrative numbers, not observed sales.
A reproducible wrangling sequence
**Define the question and row grain.** State the decision or analysis and what one output row should represent.
**Inspect sources and definitions.** Check origin, extraction time, column names, types, units, time zone, keys, and allowed values; preserve the raw source.
**Choose a useful shape.** Decide whether to unpivot observations into rows or pivot them into a summary table. Long form often helps grouped analysis; wide form may suit some reports and entry tasks.
**Combine sources deliberately.** Specify the join key, join type, and expected one-to-one or one-to-many relationship. A left join preserves the base table’s rows and exposes unmatched keys; an inner join keeps only keys present on both sides and can reduce the row count.
**Validate before and after.** Compare expected and actual row counts, duplicate and missing keys, missing values, units, ranges, totals, or rates. Check a sample against the source after joins or aggregations.
**Save the procedure and unresolved issues.** Record transformation rules and their order so the result can be recreated from the same source. Do not make unverified source values appear resolved.
Wrangling quality is not measured by the number of steps. The final table should have the row grain and meaning the question requires, and important changes or unmatched records should be explainable. A transformation running successfully does not prove that data were joined correctly.
Sources
[Hadley Wickham, “Tidy Data,” Journal of Statistical Software](https://www.jstatsoft.org/article/view/v059i10/)
[Erhard Rahm and Hong Hai Do, “Data Cleaning: Problems and Current Approaches”](https://cs.brown.edu/courses/csci2270/archives/2016/papers/Rahm2000DataCleaningProblemsand.pdf)
[pandas 3.1.1 documentation: Reshaping and pivot tables](https://pandas.pydata.org/docs/user_guide/reshaping.html)
[pandas 3.1.1 API reference: `DataFrame.melt`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.melt.html)
[pandas 3.1.1 documentation: Merge, join, concatenate and compare](https://pandas.pydata.org/docs/user_guide/merging.html)
[pandas 3.1.1 API reference: `pandas.merge`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.merge.html)