Replace Excel VLOOKUP in Python With pandas merge()
VLOOKUP in pandas is merge(). You give it the two DataFrames, the column to join on, and how to treat rows with no match. Unlike VLOOKUP it is not restricted to searching the leftmost column, and it stays fast on datasets that would make Excel crawl.
VLOOKUP and merge
VLOOKUP searches the leftmost column of a table for a value and returns a value from the same row. pandas merge() does the same for whole tables at once: it matches every row of one DataFrame with the row of another that has the same key.
Because merge works on the whole table, there is no formula to fill down, the key can be any column, and the script runs again on next month’s file without changes. It also stays fast on row counts that slow Excel down.
Install
You need pandas and openpyxl, the package pandas uses to read .xlsx files:
pip install pandas openpyxl
Load the sheets
Read each sheet into its own DataFrame. My example workbook has an Orders sheet with the main table and two lookup tables: Returns, with a return reason per order ID, and Shipping, with a number for each shipping mode.
import pandas as pd
from pathlib import Path
excel_file_path = Path.cwd() / "data" / "data.xlsx"
orders = pd.read_excel(excel_file_path, sheet_name="Orders")
returns = pd.read_excel(excel_file_path, sheet_name="Returns")
shipping = pd.read_excel(excel_file_path, sheet_name="Shipping")
If your workbook has many sheets, see the easiest way to load multiple Excel sheets in pandas.
The merge
Each lookup is one merge call: the main table, the lookup table, the key column and how=”left”. The result is a new DataFrame with the looked-up columns added, which to_excel writes into a new workbook.
# VLOOKUP the return reason: keep every order (how="left")
df1 = orders.merge(returns, left_on="Order ID", right_on="ID", how="left")
# VLOOKUP the shipping mode number on a column with the same name
df2 = df1.merge(shipping, on="Ship Mode", how="left")
df2.to_excel(excel_file_path.parent / "output.xlsx", sheet_name="Output", index=False)
how="left" is the VLOOKUP behaviour: every order stays, and orders without a return get NaN instead of #N/A. Use left_on and right_on when the key columns have different names, and on when they match. The code runs on pandas 3.1 with the example workbook from the GitHub repo, and all 3,206 orders stay in the output.
Merge types
The how argument decides which rows survive when a key has no match. VLOOKUP always behaves like a left merge. pandas gives you the other three as well.
| how | Keeps | Excel equivalent |
|---|---|---|
"left" |
All rows of the first table, matches from the second | VLOOKUP or XLOOKUP down the first table |
"right" |
All rows of the second table | VLOOKUP down the second table |
"inner" |
Only keys found in both tables | VLOOKUP, then filter out #N/A |
"outer" |
Every key from both tables | No single formula |
Troubleshooting
Two problems cover most failed lookups: duplicate keys, which add rows, and keys stored as different types, which find no matches at all.
More rows than before. If the lookup table holds the same key more than once, merge returns one row for every match, while VLOOKUP silently takes the first one. Check with returns["ID"].duplicated().sum() before merging, and drop duplicates with returns.drop_duplicates("ID") if you only want the first match. Passing validate="many_to_one" to merge makes pandas raise an error instead of quietly adding rows.
No matches. A key stored as a number in one table and as text in the other never matches. Convert both with .astype(str) first. If the keys differ by spelling rather than type, such as company names typed two ways, no conversion helps. My guide to fuzzy matching in Excel covers that case.
For a second walkthrough with the same idea, including how I switch between merge types from a dropdown, see replace your VLOOKUP with Python. The other everyday Excel formulas translate just as directly: see SUMIFS, COUNTIFS and AVERAGEIFS in pandas.
Resources
- Example workbook and code on GitHub
- pandas documentation for DataFrame.merge
- Tabellenfit: interactive SVERWEIS (German VLOOKUP) practice
