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 type | Keeps | Typical use | Watch out for |
|---|---|---|---|
| Inner join | Only rows whose key exists in both tables | Analysis limited to matched records | Silently drops unmatched rows |
| Left join | All rows from the left table, matched data from the right | Enrich a main table with extra attributes | Empty values where no match exists |
| Right join | All rows from the right table | Same as left join with tables swapped | Easy to confuse with left join |
| Full outer join | All rows from both tables | Reconciliation: find what is missing on each side | Many empty cells; keys may need coalescing |
| Append / union | All rows, stacked | Combining files with the same columns | Mismatched 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.
- One-to-one: key unique on both sides. Row count stays the same or shrinks.
- One-to-many: key unique on one side. Row count matches the "many" side. Usually intended.
- Many-to-many: key duplicated on both sides. Row count multiplies. Almost always a mistake.
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
- Both keys have the same type and format after trimming whitespace and normalising case.
- Key uniqueness is known on each side, so you know whether to expect one-to-one, one-to-many or many-to-many.
- Match rate is reasonable; a very low match rate points to a format problem.
- Column names that collide are renamed so you know which table each came from.
- Units and currencies agree between tables before values are combined.
- For ML, no column brought in from the second table describes events after the prediction moment.
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
- Upload both files: CSV, TSV, JSON, Parquet or Excel. Each sheet of a workbook becomes its own dataset.
- Clean the key columns first in the AI data editor, for example "trim whitespace and uppercase the sku column", and cast numeric IDs consistently.
- Open dataset merge and select the two datasets. Review the discovered join key candidates and the schema matching.
- Read the readiness check and fix anything it flags.
- Combine the datasets, then re-profile the result: row count, duplicate rows, missing % introduced by unmatched rows.
- Continue with analysis or pick a target and train a model.
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.
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.