The same SKU can have different prices at two sites. Match both the site and the SKU so a North order receives the North price rather than another site’s price for that product.

Pass the two column names to merge as a list. This fictional example keeps all three order rows, attaches two matching prices, and leaves one unknown price visible.

Order O1 for North and SKU 0042 receives price 3. Order O2 for South and the same SKU receives price 5. Order O3 for North and SKU 9999 has no lookup pair and keeps an unknown price.
The site and SKU together identify the lookup row. All three fictional orders remain visible, including the unmatched pair.Credit: MathBlog

Define what makes one lookup key

Each order has an order number, a site, a product code, and a quantity. The price table has one price for each site-and-SKU pair. Codes such as 0042 are text; their leading zeros belong to the identifier.

Orders to retain
order site sku units
O1 North 0042 2
O2 South 0042 1
O3 North 9999 4
Price lookup, unique by the complete pair
site sku unit_price
North 0042 3
South 0042 5

SKU 0042 appears twice in the lookup. The pairs (North, 0042) and (South, 0042) are different, and each occurs once. A larger catalog can repeat either individual column while keeping the complete pair unique.

Run the merge on both columns

Run this complete block with pandas installed. All inputs are included inline. The example was executed with Python 3.11.3 and pandas 2.3.3.

import pandas as pd

orders = pd.DataFrame({
    "order": ["O1", "O2", "O3"],
    "site": ["North", "South", "North"],
    "sku": ["0042", "0042", "9999"],
    "units": [2, 1, 4],
})
prices = pd.DataFrame({
    "site": ["North", "South"],
    "sku": ["0042", "0042"],
    "unit_price": pd.Series([3, 5], dtype="Int64"),
})


def merge_site_prices(orders, prices):
    keys = ["site", "sku"]
    for table in [orders, prices]:
        if table.columns.duplicated().any():
            raise ValueError("Use distinct column names in each table.")
        for key in keys:
            if table[key].isna().any():
                raise ValueError("Review missing merge keys.")
            if table[key].astype("string").str.strip().eq("").any():
                raise ValueError("Review blank merge keys.")
    return orders.merge(
        prices, on=keys, how="left", sort=False,
        validate="many_to_one", indicator=True,
    )


matched = merge_site_prices(orders, prices)
print(matched.to_string(index=False))
print("Rows after merging only on SKU:", len(orders.merge(prices, on="sku", how="left")))

Output:

order  site  sku  units  unit_price    _merge
   O1 North 0042      2           3      both
   O2 South 0042      1           5      both
   O3 North 9999      4        <NA> left_only
Rows after merging only on SKU: 5

Check the rows and unmatched pairs

Order O1 matches the North price of 3. O2 matches the South price of 5. O3’s North-and-9999 pair has no entry in the lookup, so its price is <NA> and its indicator is left_only.

The left merge retains the three orders in their input order. sort=False avoids requesting a sort of the join keys. The merge uses the two named columns, not the original DataFrame index.

The _merge value both confirms that a key pair was found. It does not establish that the matched price itself is present or correct. Check price values separately before calculating order amounts. An unmatched price should not automatically become zero.

See why matching only the SKU produces five rows

The final print statement deliberately makes the wrong lookup for this task. Joining only on SKU makes O1 match both price rows. O2 also matches both rows, and O3 stays as one unmatched row. That produces five rows from three orders.

Use on=["site", "sku"] to require both fields to agree in the same lookup row. The method already represents the pair; there is no need to build a combined string with a delimiter that might also appear inside a real value.

Reject ambiguous or missing keys

validate="many_to_one" permits several orders to use the same complete pair but requires unique pairs in the price table. If a second North-and-0042 lookup row appears, pandas raises MergeError. Investigate the competing records rather than keeping an arbitrary first row.

Checking SKU uniqueness alone would reject the valid two-site price table. Checking only site uniqueness would not describe a catalog that carries many products per site. Validate the combination that the business rule actually identifies.

The helper rejects missing and blank values in either key column. pandas can match null keys to other null keys, so allowing an unknown site or SKU into both inputs could attach a price for the wrong reason. Duplicate column names are also rejected before the merge.

Other key text is preserved. Case and surrounding spaces remain meaningful unless the source specification says to normalize them. Keep the same key types in both tables; converting an already numeric code to text cannot recover lost leading zeros.

An empty lookup leaves every order unmatched. An empty orders table produces an empty result when the expected columns are present. These outcomes keep the main table’s row count rather than inventing matching records.

For a simpler lookup with one identifier, see how to match two lists in pandas. Choose the key from what identifies a record, then verify row counts and unmatched pairs after the merge.

Further reading