Aug 28, 2021

vlookup with Python

VLOOKUP stands for 'Vertical Lookup'. It is an indispensible function that makes Excel search for a certain value in a 'table array', in order to return a value from a different column in the same row.

Here's a sample of Python script that can do the same thing like Excel. 


import pandas as pd
import numpy as np

workbook = 'data.xlsx'
excelfile = pd.ExcelFile(workbook)
sheets = excelfile.sheet_names

orders = pd.read_excel(workbook, sheet_name = 'Orders')
returns = pd.read_excel(workbook, sheet_name = 'Returns')

df1 = orders.merge(returns, left_on='Order ID', right_on='ID', how='left')

# Export to NEW excel workbook
output = 'output.xlsx'
df2.to_excel(output, sheet_name='Output', index=False)


Below are the 2 links where I learned from. 

Links:

  • https://www.youtube.com/watch?v=cRELNmDpaks
  • https://www.youtube.com/watch?v=AHS925L8JVk