Python Vlookup
Python Vlookup
Shiksha Online
Updated on Jan 24, 2023 11:30 IST
VLOOKUP is a common Excel function that stands for ‘Vertical Lookup’. The article
discusses the use of VLOOKUP in Pandas.
We already know that Pandas DataFrames are tabular data structures that store
data similar to an Excel or CSV file – in rows and columns. VLOOKUP is a common
Excel function that is essentially used for vertically arranged data and allows you to
map data from one table to another. In Pandas, VLOOKUP merges two DataFrames
if both have a common attribute (column). You can perform VLOOKUP in Pandas
using map() and merge() methods as discussed in this article:
T he map() method
T he merge() method
For our purpose today, let’s create a sample DataFrame as shown below:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
df
Our dummy dataset comprises of 4 columns – ‘name’, ‘age’, ‘gender’, and ‘birthmonth’.
As you can observe, it contains both numerical and categorical variables.
Now, let’s see how we can emulate using the VLOOKUP function in Pandas through
this dataset.
The pandas .map() method allows us to map values to a Pandas Series, or a column
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
in a Pandas DataFrame. This can be done using a dictionary, where the key is the
corresponding value in our Pandas column and the value is the value that we want to
map into it.
To understand this better, let’s create a dictionary that contains our mapping
values:
Copy code
Now, we will apply the map() method to the column that we want to map into:
df[‘birthmonth’] = df[‘birthmonth’].map(birthmonth_map)
df
But what if the data is stored in another DataFrame, as is when working with
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
relational databases like SQL? In such cases, instead of working with Python
dictionaries, we use the merge() method.
In the DataFrame we created above, we have a column ‘age’ that corresponds to the
year a child was born in. Let’s create another DataFrame that contains the mapping
values (birth year) for the age:
Copy code
df 2
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Now, let’s see how we can merge the two different DataFrames using the merge()
method:
Copy code
Note that VLOOKUP is essentially a left join between two tables, that is, the output
consists of all the rows in the left table and only the matched rows from the right
table.
T he arguments left and right are positional parameters that choose which DataFrames to
use as your lef t and right tables in the join.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
T he how parameter sets how the tables have to be joined: lef t, right, inner, or outer.
In the right join, the output DataFrame consists of all the rows in the right
DataFrame and only the matched rows from the left DataFrame. The unmatched
rows will be replaced by NaN values.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
By setting the how parameter to inner, the final DataFrame will contain only the rows
for which the condition is satisfied in both the DataFrames.
Copy code
By setting the how parameter to the outer, the final DataFrame will contain rows
from both the DataFrames. If rows are matched, values will be shown. If rows do not
match, NaN will be displayed.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Endnotes
T op T rending Articles:
Data Analyst Interview Questions | Data Science Interview Questions | Machine Learning
Applications | Big Data vs Machine Learning | Data Scientist vs Data Analyst | How to Become
a Data Analyst | Data Science vs. Big Data vs. Data Analytics | What is Data Science | What is
a Data Scientist | What is Data Analyst
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.