Pandasdrill

XLOOKUP in pandas

The pandas version of XLOOKUP (and VLOOKUP) is a left merge: keep every row of your table and bring in the matching value from a lookup table. For a single column, map does the same in one line.

The basic lookup

The examples use lines (order_id, sku, qty) and pricelist (sku, price, name). Product D is not in the price list.

Bring one column into each row

A left merge keeps every row of lines and adds the matching price. Without how="left" the merge is an inner join and the row with no match disappears.

Excel: =XLOOKUP(B2, pricelist!A:A, pricelist!B:B)

pandas

lines.merge(pricelist[["sku", "price"]], on="sku", how="left")

Result:

   order_id sku  qty  price
0         1   A    2   10.0
1         2   B    1   25.0
2         3   D    4    NaN
3         4   A    3   10.0

Write it yourself: Excel XLOOKUP: bring a price into each row, checked on two datasets.

The one-line version with map

Turn the lookup table into a Series indexed by the key and map the key column through it. This suits a single return column.

Excel: =XLOOKUP(B2, pricelist!A:A, pricelist!B:B)

pandas

lines.assign(price=lines["sku"].map(pricelist.set_index("sku")["price"]))

Result:

   order_id sku  qty  price
0         1   A    2   10.0
1         2   B    1   25.0
2         3   D    4    NaN
3         4   A    3   10.0

Return several columns at once

Select the columns you want from the lookup table before merging. One merge brings all of them.

Excel: =XLOOKUP(B2, pricelist!A:A, pricelist!B:C)

pandas

lines.merge(pricelist, on="sku", how="left")

Result:

   order_id sku  qty  price    name
0         1   A    2   10.0  Anchor
1         2   B    1   25.0    Bolt
2         3   D    4    NaN     NaN
3         4   A    3   10.0  Anchor

Try it yourself

In Excel you would add a column with XLOOKUP(sku, pricelist). Do the same in pandas: return lines with one extra column, price, looked up from pricelist by sku. Keep every row of lines in its original order. A product that is not in the price list gets a missing price.

lines: 6 rows, first 4 shown

order_idskuqty
1"A"2
2"B"1
3"D"4
4"C"3

pricelist: 3 rows, first 4 shown

skuprice
"C"4
"A"10
"B"25

Free, no account needed. Real pandas runs in your browser; the first run takes a few seconds to load Python.

When there is no match

Excel shows #N/A, or the if_not_found argument. pandas writes NaN, the missing value.

Use a default instead of a missing value

fillna after the lookup plays the part of if_not_found.

Excel: =XLOOKUP(B2, pricelist!A:A, pricelist!B:B, 0)

pandas

lines.assign(price=lines["sku"].map(pricelist.set_index("sku")["price"]).fillna(0))

Result:

   order_id sku  qty  price
0         1   A    2   10.0
1         2   B    1   25.0
2         3   D    4    0.0
3         4   A    3   10.0

List the rows that found nothing

indicator=True adds a column that says where each row came from, so the misses are easy to pull out.

m = lines.merge(pricelist[["sku", "price"]], on="sku", how="left", indicator=True)
m[m["_merge"] == "left_only"]

Result:

   order_id sku  qty  price     _merge
2         3   D    4    NaN  left_only

Write it yourself: Orders with no customer, via indicator, checked on two datasets.

The trap: a repeated key

XLOOKUP returns the first match and stops. merge returns every match, so a key that appears twice in the lookup table duplicates your rows and inflates any total you compute afterwards.

See the duplication

Four order lines become five because product B is listed twice.

lines.merge(dup_prices, on="sku", how="left")

Result:

   order_id sku  qty  price
0         1   A    2   10.0
1         2   B    1   25.0
2         2   B    1   25.0
3         3   D    4    NaN
4         4   A    3   10.0

Write it yourself: A join that inflates the total, checked on two datasets.

Guard against it

Remove the repeated rows first, or pass validate="many_to_one" so pandas raises an error instead of quietly duplicating.

lines.merge(dup_prices.drop_duplicates(), on="sku", how="left", validate="many_to_one")

Result:

   order_id sku  qty  price
0         1   A    2   10.0
1         2   B    1   25.0
2         3   D    4    NaN
3         4   A    3   10.0

Approximate match

XLOOKUP with match_mode -1 finds the largest value that is less than or equal to the lookup value, the usual way to read a tier or tax table. In pandas that is merge_asof, and both tables must be sorted by the key.

Look up a discount tier

Each order gets the tier whose min_qty is the largest one not above its units.

Excel: =XLOOKUP(B2, tiers!A:A, tiers!B:B, , -1)

pandas

pd.merge_asof(qty.sort_values("units"), tiers, left_on="units", right_on="min_qty")

Result:

  order  units  min_qty  discount
0     x      4        0      0.00
1     y     12       10      0.05
2     z     75       50      0.10

Practice these live

Each exercise runs real pandas in your browser and checks your result on the sample and on a second dataset. Your first 8 are free, with no account.

Start practicing free

Related guides