Urgent.News

What's breaking now, across thousands of outlets.

Tech

pandas merge() Returned More Rows Than You Started With? Catch Duplicate Keys with validate=

You left-joined 4 orders onto a customer table and got back 6 rows . Worse, the revenue column went from 520 to 830, and nobody noticed until the report reached a manager. merge() did exactly what it was told. A left join keeps every left row, but if a key appears twice on the right , every matching left row is copied once per match. No error, no warning, just quietly inflated numbers. Here is…

When performing a left join using the merge() function in pandas, it is possible to end up with more rows than the original dataset contained. This can occur when duplicate keys are present in the lookup table, causing every matching left row to be copied for each match. In the example provided, joining 4 orders onto a customer table with duplicate customer IDs resulted in 6 rows instead of the original 4, and the revenue column increased from 520 to 830 without any warning or error.

To catch such duplicate key issues, there are three key steps. First, look at the lookup table to identify the duplicate keys. This can be done by using the value_counts() method on the customer_id column and filtering for values greater than 1. This reveals the rows in the lookup table that cause the duplication issue. In the example, customer_id '10' appears twice, with different names and cities associated with it.

Second, prevent the bad merge by setting the validate= argument in the merge() function. This argument checks the relationship you expect between the two datasets and raises an error if the data disagrees. In the example, the expected merge is a many_to_one merge, which can be enforced by setting validate='many_to_one'. This will cause the merge() function to raise a MergeError if duplicate keys are found in the lookup table.

Third, fix the data rather than hiding the symptom. Simply calling drop_duplicates() on the result does not address the underlying issue and may drop legitimate rows. Instead, identify which lookup row should be kept as the correct one, remove duplicates from the right-side dataset (customers), and then merge the datasets again with the validate= argument set to 'many_to_one'.

This ensures that the correct data is used and the merge is performed correctly. If there is an updated_at column, sort by it before dropping duplicates to ensure that the most recent row is kept.

An alternative approach is to add an indicator column to the merged dataset using the indicator=True argument. This will show which rows actually matched during the merge, helping to identify any missing matches. In the provided example, an 'sku' column was added to the orders DataFrame to demonstrate the use of indicator. By adding this indicator, it becomes easier to spot any discrepancies in the merged dataset.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Wednesday 4 November →