tensorturnBETA
Home › Data Cleaning & Analytics

How to Merge Two Datasets (and Join Two CSV Files) Correctly

To merge two datasets, pick a join key that identifies the same entity in both tables, make sure it has the same type and format on each side, check whether it is unique, then choose a join type (inner, left, right or full outer) that keeps the rows you need. TensorTurn's dataset merge discovers likely join keys automatically, matches schemas between the two files and runs a readiness check before combining them, which catches problems like duplicated keys that multiply your row count.

Merge, join and append are different things

"Merging" is used loosely. A join combines two tables side by side by matching a key: customers plus their orders, products plus their prices. An append (or union) stacks two tables with the same columns on top of each other: January sales plus February sales. Joining needs a good key. Appending needs matching schemas, meaning the same columns with the same names and types. Many real merges involve a bit of both, which is why schema matching matters even for joins.

Join types explained

Join typeKeepsTypical useWatch out for
Inner joinOnly rows whose key exists in both tablesAnalysis limited to matched recordsSilently drops unmatched rows
Left joinAll rows from the left table, matched data from the rightEnrich a main table with extra attributesEmpty values where no match exists
Right joinAll rows from the right tableSame as left join with tables swappedEasy to confuse with left join
Full outer joinAll rows from both tablesReconciliation: find what is missing on each sideMany empty cells; keys may need coalescing
Append / unionAll rows, stackedCombining files with the same columnsMismatched column names or types

For machine learning, the left join is the usual choice: your labelled table is the left side, and you add features from other tables without losing labelled rows.

Finding the right join key

The best key is a stable identifier that means the same thing in both tables, such as customer_id, order_id or SKU. Names and emails work but are fragile. Before joining, check three things. First, overlap: what share of keys in one table appear in the other? Low overlap usually means a format mismatch, not missing data. Second, type and format: "00123" as text and 123 as a number will not match, nor will "ABC-1" and "abc-1 ". Third, uniqueness: is the key unique on each side? TensorTurn's merge discovers candidate join keys automatically, so you do not have to guess which columns correspond.

The row explosion problem

The most common merge mistake is joining on a key that is duplicated in both tables. A join returns one output row for every matching pair. If customer 42 appears 3 times in the orders table and 4 times in a support tickets table, joining the two on customer_id produces 12 rows for that customer, and every order is now counted 4 times. Across a whole dataset, a many-to-many join can turn 10,000 rows into hundreds of thousands, inflating sums and creating duplicates that leak across train and test splits.

The fix is to aggregate the "many" side first (for example, count tickets per customer), or to add a second key column so the match becomes unique. Always compare row counts before and after a join.

Schema matching

When two files describe the same thing they rarely use the same column names: "cust_id" vs "CustomerID", "amount" vs "total_amount". Schema matching lines up corresponding columns so they are treated as one field, and flags columns that exist on only one side. TensorTurn's merge matches schemas between the two datasets automatically, which is especially useful when appending exports from different systems or periods.

Readiness checks before you merge

TensorTurn runs a readiness check as part of the merge so these issues show up before the combined dataset exists, not after a report double-counts revenue.

How to merge datasets on TensorTurn

Doing it with other tools

In Excel, XLOOKUP or Power Query's Merge Queries handle joins, with Power Query better for anything repeated. In pandas, merge with the validate argument (for example "one_to_many") raises an error if the key relationship is not what you expect, which is a good habit. In SQL, a JOIN with a prior GROUP BY on the many side avoids row explosion.

Start building free

Frequently asked questions

How do I join two CSV files?

Find a column that identifies the same record in both files, clean it so values match exactly, then join on it, usually with a left join that keeps every row of your main file. In TensorTurn you upload both files and the merge feature discovers the join key, matches schemas and runs a readiness check.

Why did my row count increase after merging?

The join key is duplicated in both tables, so every matching pair produced a row. Aggregate the many side first or add a second key column to make the match unique, and compare row counts before and after every join.

What is the difference between an inner join and a left join?

An inner join keeps only rows whose key appears in both tables. A left join keeps every row of the left table and fills in empty values where the right table has no match.

Why are my keys not matching when the values look the same?

Usually the formats differ: trailing spaces, different case, leading zeros dropped from a numeric ID, or text versus number types. Trim, normalise case and cast both key columns to the same type before joining.

Can TensorTurn merge more than two datasets?

The merge feature combines two datasets at a time. To combine more, merge two, then merge the result with the next dataset, re-checking the row count each time.