Pandas How To Uncategorized How to vlookup in Pandas

How to vlookup in Pandas

In this article I try to show how to vlookup in Pandas.

The equivalent of a VLOOKUP in pandas is the merge method, which allows you to join two DataFrames based on a common column. By default, merge performs a left join, meaning that the resulting DataFrame will contain all the rows from the left DataFrame and any matching rows from the right DataFrame. If there are non-matching rows in the right DataFrame, they will be filled with NaN values:

import pandas as pd

df1 = pd.DataFrame({'key': [1, 2, 3], 'value': ['A', 'B', 'C']})
df2 = pd.DataFrame({'key': [2, 3, 4], 'value': ['D', 'E', 'F']})

result = df1.merge(df2, on='key', how='left')


In this example, the merge method will return a DataFrame with the following values:

   key value_x value_y
0   1      A    NaN
1   2      B      D
2   3      C      E

In the resulting DataFrame, the values from df1 are stored in the value_x column and the values from df2 are stored in the value_y column. The rows with a matching key value have both columns filled with values, while the non-matching rows have the value_y column filled with NaN.

See also:
How to write to existing Excel file in Pandas
How to save dataframe as Excel file
How to merge two dataframes


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post