Microsoft Excel Tutorial: VLOOKUP in Python for Excel
How to do a VLOOKUP or XLOOKUP in Python.
To download the examples in this workbook: www.mrexcel.com/youtube/h6__f...
Welcome to another video on Excel Python XLOOKUP! In this video, we will be exploring how to do an XLOOKUP or a VLOOKUP in Python, and it's remarkably easy. We will start with a simple VLOOKUP and then move on to something more complex, like not specifying the key field. We will also cover what to do when a customer is missing from the lookup table, which is essentially the IFERROR equivalent. Additionally, we will learn how to limit which fields are returned, something that we don't have to worry about with VLOOKUP. We will also address common issues such as mismatched headings and duplicated customers.
Before we dive into the code, let's talk about the comment indicator. The hash symbol allows you to comment a line and explain what the next line is. In this video, I have used this to demonstrate a problem and then provide the solution. This allows me to show the problem and then quickly switch to the solution without having to run the code again. Now, let's get started!
We have a large data set on the left-hand side and a small lookup table on the right. Our goal is to get the sector field from the lookup table and add it to the data set. To do this, we will use the pd.merge function, which is similar to a database join. We will specify the left table, the lookup table, and the column that is in common between them. We can also choose the join type, which is similar to Power Query. In this example, we will use Left join. The result will be our original fields, along with the new field from the lookup table - Sector. And the best part? We don't even have to specify the "on" parameter because the fields have the same name.
But what happens if a customer is missing from the lookup table? We will use the .fillna method to get rid of the #HUM! error and replace it with blanks. Additionally, we will learn how to remove unnecessary fields from the lookup table using the .drop method. And for those of you who are used to Excel's VLOOKUP, we will address the issue of duplicated customers and how to handle them in Python.
But what if we have two keys? In this case, we will use the left_on and right_on parameters to specify the fields that are in common between the two tables. And just like that, we have successfully joined two tables in Python using the pd.merge function. This is a great building block for more complex tasks, and I hope to eventually build a beautiful mansion of code using these building blocks.
Thank you for watching this video on Excel Python XLOOKUP. If you enjoyed it, please don't forget to Like, Subscribe, and Ring the Bell. And as always, feel free to leave any questions or comments down below. See you next time for another netcast with MrExcel!
Buy Bill Jelen's latest Excel book: www.mrexcel.com/products/latest/
You can help my channel by clicking Like or commenting below: www.mrexcel.com/like-mrexcel-...
Along the way, you will see:
101: Doing a VLOOKUP
Not specifying the key field!
What if a customer is missing from lookup table? How to IFERROR()
Limiting which fields are returned.
What if headings don't match?
What if a customer is duplicated?
Lookup on two fields.
Table of Contents
(0:00) Python lookup overview
(0:25) Comment indicator in Python
(1:00) VLOOKUP 101 in Python using pd.merge
(2:36) Leaving off the On field
(2:50) IFERROR when customer missing with .FillNA
(3:23) Limiting lookup table to needed fields
(4:04) Headings don't match left_on and right_on!
(4:50) .drop method to remove a column from Python
(5:12) If duplicate in lookup table
(6:00) drop_duplicates to remove duplicates
(7:06) What is the right way to show duplicates
(7:29) Lookup on two fields
(8:11) Final thoughts on pd.merge
(8:50) Building blocks in Python
(9:28) Nancy Faust
Join the MrExcel Message Board discussion about this video at www.mrexcel.com/board/threads...
14 июл 2024