Most people who search for a fix to the _x and _y columns after a pandas merge are looking for a renaming trick. They find one quickly: pass suffixes=("", "_drop"), filter out anything ending in _drop, and move on.
The DataFrame looks clean, the notebook runs, and the actual problem stays in the data. The suffixes were never a naming problem. They are pandas telling you that two tables share a column that is not part of the join key, and that someone has to decide what that shared column means.
That decision takes about a minute per column once you know the three possible answers. Skipping it is how a monthly revenue report ends up with customer emails from the wrong system, or how a take-home exercise for a data analyst role in Lisbon gets marked down for silently dropping half the information. This memo explains the mechanism, gives a decision procedure, walks through an illustrative case and closes with the situations where the usual fix is the wrong one.
What the suffixes are actually telling you
When you call pd.merge(left, right, on="customer_id"), pandas matches rows on customer_id and then places every other column from both tables side by side. If both tables also have a column called email, pandas cannot keep two columns with the same name, so it appends the default suffixes and you get email_x from the left table and email_y from the right.
import pandas as pd
crm = pd.DataFrame({
"customer_id": [101, 102, 103],
"email": ["ana@example.pt", "rui@example.pt", None],
"country": ["PT", "PT", "ES"],
})
billing = pd.DataFrame({
"customer_id": [101, 102, 104],
"email": ["ana.silva@example.pt", "rui@example.pt", "eva@example.es"],
"amount_eur": [120.0, 75.5, 40.0],
})
out = pd.merge(crm, billing, on="customer_id", how="left")
print(out.columns.tolist()) # customer_id, email_x, country, email_y, amount_eur
Three details matter. First, only overlapping non-key columns get suffixes; the key in on appears once. Second, if you join with left_on and right_on using different names, both key columns are kept as they are. Third, the suffixes are applied per merge. In a chain of three merges, the third table's email does not overlap with email_x or email_y, so it arrives as plain email, and you end up with three versions of one attribute under three unrelated names.
The toy example already shows why renaming is risky. Of the 3 CRM customers, 2 match billing. For customer 101 the two emails differ, for 102 they agree, and for 103 the CRM has no email while billing has no row at all.
Keep email_x and you lose the billing address for 101; keep email_y and you get an empty value for 103. Scale that to 50,000 customers where perhaps 8 percent of emails differ between systems, and a blind rename silently picks the wrong value for about 4,000 people.
So when _x and _y appear, there are only three things that can be true. The shared column is really part of the key and you left it out of on. The shared column is the same attribute coming from two sources, and you need to decide which source wins. Or the two columns share a name but mean different things, and they deserve different names.
The decision procedure
The small change is this: decide what every overlapping column is before you call merge, not after. In practice it takes the following steps.
- List the overlap explicitly:
overlap = set(left.columns) & set(right.columns) - {"customer_id"}. If the set is empty, you are done and no suffixes will appear. - For each column in the overlap, check whether it belongs in the key. Ask: would two rows with the same
customer_idbut different values in this column be different records? For columns likecountry,dateoraccount_typein multi part keys, the answer is often yes. Then add it toon. - If the column is the same attribute from two sources, pick the authoritative source. Billing usually wins for invoicing addresses, the CRM for marketing consent. Select only the columns you need from the other table before merging, for example
billing[["customer_id", "amount_eur"]]. - If you need a fallback, merge with explicit, meaningful suffixes and combine:
suffixes=("_crm", "_billing"), thenout["email"] = out["email_crm"].combine_first(out["email_billing"]). - If the columns mean different things, rename one side before the merge:
billing.rename(columns={"email": "invoice_email"}). - Add
validate="one_to_one"orvalidate="many_to_one"to the merge, so pandas raises an error if the key you chose is not as unique as you think. - Use
indicator=Trueonce while developing to see how many rows matched on both sides, then remove it.
billing_slim = billing[["customer_id", "amount_eur"]]
out = pd.merge(
crm, billing_slim,
on="customer_id", how="left",
validate="one_to_one",
)
One more habit belongs in the procedure: compare row counts. With how="left" and a many_to_one or one_to_one relationship, the output must have exactly as many rows as the left table. If the left table has 48,000 rows and the merge returns 51,200, at least 3,200 rows were duplicated by a non unique key on the right. An assert len(out) == len(left) costs 1 line and catches this on every run.
That final version has no suffixes because there is no overlap, and the validate argument documents your assumption about the key. A reviewer reading the code sees both decisions in two lines.
An illustrative case from a take-home exercise
The following case is illustrative, based on a common shape of take-home tasks for analyst and data engineering roles. A candidate applying to a Porto e-commerce company received three CSV files: orders (about 48,000 rows), customers (about 9,500 rows) and a returns file (about 3,100 rows). The task was to compute net revenue per country for the last quarter.
All three files had a status column. In orders it meant shipped, cancelled or pending. In customers it meant active or closed. In returns it meant approved or rejected. Orders and customers also both had country: the shipping country on orders, the country of registration on customers, and the brief defined "country" as the second one.
The candidate used the renaming trick from the opening of this memo: suffixes=("", "_drop") on every merge, then dropped every column ending in _drop. Pandas raised no error. But the trick kept the left table's version of each overlapping column every time. After the first merge, country was the shipping country, not the registration country.
After the returns merge, the returns status was the one dropped, so the candidate could no longer tell approved returns from rejected ones and subtracted both. Net revenue came out about 6 percent lower than the expected answer, with two countries swapped in the ranking.
A second candidate on the same task ran the overlap check first, renamed the three columns to order_status, customer_status and return_status before any merge, and used validate="many_to_one" for the orders to customers join. That merge failed immediately: the customers file contained 14 duplicate customer_id values, from accounts that had been closed and reopened.
The second candidate wrote two sentences about how they deduplicated and why. In the debrief, the hiring team said that paragraph mattered more than the final number. Data teams in Lisbon and Porto, like most teams, hire for the habit of checking assumptions, and a take-home task is often designed to reveal it.
Mistakes to avoid, and when the cleanup is the wrong move
The recurring mistakes with suffixes are easy to recognise:
- Dropping one side by default, for example keeping all
_xcolumns, without checking which table is authoritative for each attribute. - Passing
suffixes=("", ""), hoping the columns will merge. Pandas cannot create two columns with the same name this way and will refuse the merge, so the attempt fails instead of combining anything. - Renaming after the merge instead of before, which breaks as soon as a later merge introduces a third version.
- Leaving the overlap in the key out of
on, which creates a many to many join and multiplies rows. If row count jumps after a merge, check this first. - Using
df.joinwithoutlsuffixandrsuffixand being surprised by the overlap error;joinasks you to decide wheremergedecides for you. - Trusting the merge without
validate, so duplicated keys inflate totals quietly. - Checking totals only at the very end. Compare the sum of a numeric column such as
amount_eurbefore and after each merge; a drift of 0.5 percent is easy to explain at step 1 of a pipeline and very hard to trace at step 4.
There are also situations where you should not apply the cleanup at all. Do not slim the right table down when the task is reconciliation: if you are comparing CRM and billing to find mismatched emails, you want both columns, with clear suffixes like _crm and _billing, and a comparison column on top. Do not force a column into the key when its values are messy, such as free text country names; standardise first or the merge will drop matching rows.
Do not use combine_first for attributes where an empty value is meaningful, such as a withdrawn marketing consent, because a fallback would quietly overwrite a deliberate blank. And do not spend this effort on a one off exploration you will delete within the hour: the default suffixes are fine for a quick look. The rule earns its keep in code that runs again next month, or code someone else will review.