Lambda Functions for Data Science / Data Analysis - Python P.6

Luke Barousse · Beginner ·📊 Data Analytics & Business Intelligence ·5y ago

Key Takeaways

This video covers the use of lambda functions in Python for data science and data analysis, including sorting dictionaries, creating anonymous functions, and using filter and map functions.

Full Transcript

what up dad nerds i'm luke and welcome to my channel where i make data visualization easy in this video today we're going to be talking about lambda functions also known as anonymous functions and for this it's an alternative to just normal functions where normal functions take a minimum of uh two or more lines to make with this you can write a function within a single line and make it more concise and thus stick to the readability aspect of python so for this we're gonna be covering three main topic areas the first thing i think it's important to understand the use case of land of functions and where you're going to be seeing this out in the wild two we're going to be moving into actually writing lambda functions as simple as possible and understanding their structure and then the third portion we're going to get into actually applying it and using in ways that i find that i use it quite frequently for data analysis so with that let's jump right into my computer so here we are in my mac with vs code up and a jupiter notebook running and the first thing we're gonna be going over is the typical use cases of lambda and so for this in the last video we were we did we were talking about functions and we were actually able to dive into the office data set which i have included a link below but we went through when we basically counted the number of lines each of the actors had from the office and what we did is we put it into this dictionary which i have right here at the bottom and it shows you the different lines that each of the actors had what i frequently accomplished in that analysis though is well i have this but now i need to sort this list or sort this dictionary and specifically i need to sort it by the values and so that's the task that we have and that's where lambda actually comes into play is is using and sorting this so in order to sort this dictionary it's very common to use the sorted function and what it does is you provide it two values to the sorted function the first one being the interval the thing that you want to iterate through in order to sort and then key the second argument which you can specify and it will outline which item within a dictionary or a tuple or a list that you want to focus on to sort on so keep in mind right we want to we want to sort on the values so we're going to have to build some sort of function that pills out the values for this but first before we get into that let's talk about the the iterable first so looking at what we have we have actor lines and do shift enter and yeah it's a dictionary but dictionary necessarily isn't uh necessarily something we can iterate through so we'll use the actor lines dot items method and what this will provide us is a list of tuples so michael one two three three seven is a tuple pam five three seven five is a tuple and so on so this is a more of an iterable that we want to use and from this we want to actually get a sort by this second item in the list so just going back to the function we've defined we're going to be using the sorted function we've defined what the interval is it's going to be actors lines dot item now we have to do the function that's going to pull out this second item in the list so trying to start with a function we're going to go we'll put in sorted and just to recap we're gonna be using the actor lines.items as the interval and then from there we're gonna do key equal to and we need to apply some sort of function right here so in order to get what we want we're going to have to make a function for this so we'll say we'll define the function and we'll say it's get values and we're providing it some sort of item and from there we want to return item and we want to return the first uh or sorry the number one index win number one index or the second item so in this case we're wanting to return the one two one three seven so we have this function now get values so we'll put this function in here so get values and then if i run this shift enter what i will get is the values above um so these dictionary items or those actor lines uh now sorted from lowest to highest and i can actually if i wanted to i could put it in uh put in this an extra argument or reverse change this to true and shift enter and it will reverse the order so anyway so i have this but i have to write as you can see i had to write this function outside of it so the readability of this statement is not that good so let's see how we can use lambda functions the lambda use case in order to make this more concise and readable and easier to implement okay so for this we're going to still use the the sorted function and we're still using that actor lines dot items and then for the function for so we have key equals to and remember above we inserted this function get values into here but for this one we're just going to insert the lambda function right into here and we want uh it's going to be the item and item one and so this is the the same function that we have above and we're going to go into more detail on this here in a little bit but i just wanted to show and if i do shift enter same thing gets the values and then if i wanted to uh change it in the different order i can just set reverse equal to true and now michael's up at top but the main point is this uh and it's fine if you don't understand just checks we're going to go over lambda functions but we can see that this is much more concise and also easier to read once you understand lambda functions in order to sort some sort of dictionary or list vice using this which isn't as concise and maybe harder to read and may also cause you to delete this function and have problems later on so moving on into lambda the basics of lambdas let's look at a simple function and then how this would be written in a lambda function to make it even more concise so let's say we had a function called return number and this is a v1 and for this we're just going to input some sort of number and i'll put num and if i didn't mean to evaluate that we go we get the next line we go to return num so for this all we want to do is input a number and then return that same number so if i were to run this of return number v1 and i input number one shift enter i would get back one and but this is two lines of code it's not necessarily concise so let's look at how we'd use this with lambda so for lambda you would do a you would start it with lambda and then for the input value you put right next to it so you'd put num and then you'd put this colon and then finally in this case we want to return num so whatever following the colon is basically like the return statement and shift enter we can see that hey this is a function in itself so if we assign this function to we'll say return number v2 equal to this and we do shift enter now what we can do is we can take this return number v2 and provide it in parentheses so activate this function what we want to input so in this case let's say a hundred and twelve shift enter and bam so this lambda thing can do it in a much more concise way and provides that value that we need just taking this a step further i mean we can also go as far as to just put this lambda function within parentheses and we don't even have to assign it we can make maintain the anonymous uh nominicity about it i don't know um so that this is the function within parentheses and then we can feed it whatever we want so we want to feed it this i don't know 36 doing shift enter and we can see hey this function and then it's returning that 36 as we wanted so moving on though let's say we have multiple values and let's build it on it even further so let's say we want to add numbers we can do lambda and lambda x and y so we have two variables in this case that we're going to be providing into this and then from here outside of the colon we're going to do whatever operation that we want to do so we want to add these numbers so we'll do x x plus y and do shift enter to evaluate and then yeah we can do this add lambda function and then supply it the arguments that we want to activate this so let's say we want to do 4 plus 6 and do a shift enter and we can see hey add 4 6 and we get our 10. let's keep on taking this further and let's say we had this list and we routinely whenever we have these types of lists we want to return for some reason we were going to return the third value in the list so for this we could make a lambda function so get a third and we could assign it to this lambda function and for this we're going to supply it some sort of list and from this list we want the third item of that list so i'd supply that list and then in brackets i want the third item so i put two because uh python is indexed at zero and so that would give us the third item in that list and if i want to feed this list to this lambda function so actor list and then i run it shift enter we'll get gym so we'll get the third item so if say this list changes we can still get that third item whenever we feed whatever list we feed into this moving into the application portion of lambda functions so we have we're gonna have two tasks for this we're gonna have one we're going to have a list of numbers and for those lists we want to extract on the odd numbers and then for the second portion we're going to have a list of phrases and we're going to add a token statement to each one of those phrases and if you recall from my first use case it's frequent that lambda is used within functions other functions in order to provide a value so for this first one we're going to be using the first case of with a list of numbers extract only the odd numbers we're going to be using the filter function to extract the values from a list and the basic structure or syntax of this is you have the filter function and then you feed another function inside of it and then you have the iterable itself that you want to cycle through so in this case in our case it's going to be a list of numbers and what happens is it will cycle through this list and this function will match up with these intervals and whenever a value is returned as true that coordinates to an interval or an item in an interval it will maintain that item in the list so let's see what i'm saying and let's actually build it so here's the list that we're going to be using we're going to be using dwight sales and it's a a list of different numbers and for this remember we want to get the odd values odd number values from this so for this we're going to be using the filter function so we'll start with filter and then we need to input the function remember we're using lambda for this so this is going to go through and each one of the items in the interval it's going to feed to this function itself and whether we need the function to return true or false so for this lambda function we need to evaluate for the numbers that we feed to it if the number is equal to or if sorry the number divided by or the modulus of 2 is equal to 1 so therefore it is odd it will return true and then from there we'll feed the iterable so it will be dwight sales so each one of the items so this 192 is going to go through into this lambda function and from there it will divide it by 2 if it's if the remainder is equal to 1 which it won't be in that case but it won't be so it'll be false so it won't be retained in the list but when we get to an odd number it will be one it'll be true and we'll maintain that number in the list so running shift enter we get a filter object and so we need to we want to see this filter object so we can just hey say let's convert this to a list and do shift enter and bam there we have it so we just use the filter function combined with lambda in order to get only the odd numbers from dwight sales so i show this case because it's very important to understand that you're going to have in data analysis and data science you're going to have lists and other things that you need to filter for and apply a function to it this would be the easiest manner to use a lambda function combined with the filter function to accomplish this for the third portion we're going to be provided a list of phrases and we want to add a token statement to the end of it and for this we're going to be using the map function so similar above that we use filter for this we're going to be using map and what it does is you have some iterable that you want to go through so in our case it's going to be a list of phrases and we want to apply some sort of function to that to each item in the list and you'll definitely accomplish this or you'll need to do this in data analysis i find quite frequently so looking at the list of phrases um i have a list of that's what she said phrases and what we want to do is we want to for each one of these phrases with phrases within or strings within the list we want to add that's what she said to the end of it let's see how we would do that with this map function so recall map we're going to be using the function first and then the interval so going down here i'll come in i'll enter uh map and we want the function first so that'd be lambda and we're providing it some string right and we want to take that string and we want to add uh that's what she said okay so that's what the we want to do and from here we want to and now add the iterable and that is she said and evaluating up above shift enter and then evaluating right here shift enter what we get is a map object and similar to above we need it we want to see this right so we need to convert this to a list and so i put the list within this or inside of a list and bam there we go so what it has done is it's going through and for the values above for the she said it has gone through and it's added to the end of it that's what she said and this is a demonstrating a performance of operations on strings but just use your imagination you could also be using that some sort of mathematical function to apply to a row of data to a list or to a dictionary bam so there you have it that is lambda functions just to reiterate the point you're typically not going to be just using lambda functions by themself you're going to be using them in conjunction with other functions in order to map or filter or even reduce and you're going to be using them in conjunction with those so that's why it's very important to understand the basics of lambda but then also how you can apply it with other functions so as always smash that like button if you got value out of this video also if you're following along in this tutorial series for the basics of python consider subscribing and finally drop a comment down below with any questions you may have with lambda functions and with that we'll see in the next one

Original Description

Python Fundamentals Course (DataCamp) 👉🏼 https://lukeb.co/PythonBasicsDataCamp Data Analyst Track w/ Python (DataCamp) 👉🏼 https://lukeb.co/PythonAnalystDataCamp (My recommended courses that I took to learn Python!) This video provides an overview of how data scientiests and data analysts frequently use lambda within functions like sorted, map, and filter. We start this video by going over a simple use case of sorting a dictionary using the sorted() function and lambda. From there we go over the basics of lambda and finally conclude with covering some examples of using lambda to filter a list and also map a list. Playlist (Course) Material 👉🏼 https://github.com/lukebarousse/Python_Intro 00:00 Intro 00:54 Use Case: Sorting a dictionary using sorted() 03:28 Use Case: Option 1 - Utilizing a User-defined Function 05:06 Use Case: Option 2 - Utilizing a Lambda Function 06:21 Lambda Basics: Syntax and Operation 10:38 Application: Filtering a list w/ filter() 14:12 Application: Transform a list w/ map() 16:26 conclusion Basics of Python playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT8zTx9XO9jFf4PQ0iwBWfXB Setting up Python on your computer playlist 👉🏼 https://www.youtube.com/playlist?list=PL_CkpxkuPiT9udgCeqZpS4HKF6uIzra3r Recommended Certificates/Courses (Affiliate Links) ================================== DataCamp: Python 👉🏼 https://lukeb.co/PythonBasicsDataCamp Power BI 👉🏼 https://lukeb.co/PowerBIDataCamp Tableau 👉🏼 https://lukeb.co/TableauDataCamp R 👉🏼 https://lukeb.co/RDataCamp Data Analyst w/ Python 👉🏼 https://lukeb.co/PythonAnalystDataCamp DataCamp Subscription (Monthly $25USD) 👉🏼 https://lukeb.co/DataCampSub Coursera: Google Data Analytics Certificate (BEGINNERS START HERE) 👉🏼 https://lukeb.co/GoogleCert Python for Everybody 👉🏼 https://lukeb.co/PythonForEverybody SQL 👉🏼 https://lukeb.co/PostgresSQLUofM Coursera Plus Subscription (Monthly $59USD) 👉🏼 https://lukeb.co/CourseraMonthly Coursera Plus Subscription (
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Luke Barousse · Luke Barousse · 22 of 60

1 Connect Google Sheets to Tableau & Joining Data - Tableau Tutorial P.1
Connect Google Sheets to Tableau & Joining Data - Tableau Tutorial P.1
Luke Barousse
2 How To Use Tableau Desktop Controls - Tableau Tutorial P.2
How To Use Tableau Desktop Controls - Tableau Tutorial P.2
Luke Barousse
3 Dimensions Vs Measures  (Blue Vs Green Data) - Tableau Tutorial P.3
Dimensions Vs Measures (Blue Vs Green Data) - Tableau Tutorial P.3
Luke Barousse
4 Create Stacked Bar Chart (and any other visuals EASILY!) w/ Show Me! - Tableau Tutorial P.4
Create Stacked Bar Chart (and any other visuals EASILY!) w/ Show Me! - Tableau Tutorial P.4
Luke Barousse
5 Conditional Format Tables in Tableau (Like Excel!) - Tableau Tutorial P.5
Conditional Format Tables in Tableau (Like Excel!) - Tableau Tutorial P.5
Luke Barousse
6 Calculated Fields in Tableau (Formulas & IF Statements) - Tableau Tutorial P.6
Calculated Fields in Tableau (Formulas & IF Statements) - Tableau Tutorial P.6
Luke Barousse
7 Parameters (Create & Use in Calculated Fields and/or Visuals) - Tableau Tutorial P.7
Parameters (Create & Use in Calculated Fields and/or Visuals) - Tableau Tutorial P.7
Luke Barousse
8 Totals, Average Lines, & Trend Lines (Analytics Pane) - Tableau Tutorial P.8
Totals, Average Lines, & Trend Lines (Analytics Pane) - Tableau Tutorial P.8
Luke Barousse
9 How To Create a Dashboard - Tableau Tutorial P.9
How To Create a Dashboard - Tableau Tutorial P.9
Luke Barousse
10 Upload your dashboard to Tableau Public  - Tableau Tutorial P.10
Upload your dashboard to Tableau Public - Tableau Tutorial P.10
Luke Barousse
11 Install Python for Data Science on Mac & Windows (PC) with Anaconda - P.1
Install Python for Data Science on Mac & Windows (PC) with Anaconda - P.1
Luke Barousse
12 How to run Python for Data Science - Editors vs IDEs - P.2
How to run Python for Data Science - Editors vs IDEs - P.2
Luke Barousse
13 Install VS Code with Python for Data Science / Data Analysis - P.3
Install VS Code with Python for Data Science / Data Analysis - P.3
Luke Barousse
14 Understanding Virtual Environments for Data Science / Data Analysis - P.4
Understanding Virtual Environments for Data Science / Data Analysis - P.4
Luke Barousse
15 Using VS Code with Python for Data Science / Data Analysis - P.5
Using VS Code with Python for Data Science / Data Analysis - P.5
Luke Barousse
16 Python for Data Science / Analysis  ft. 'The Office' Dataset - P.0
Python for Data Science / Analysis ft. 'The Office' Dataset - P.0
Luke Barousse
17 Python Objects frequently used in Data Science / Data Analysis - P.1
Python Objects frequently used in Data Science / Data Analysis - P.1
Luke Barousse
18 Python If Statements for Data Science / Data Analysis - P.2
Python If Statements for Data Science / Data Analysis - P.2
Luke Barousse
19 Python For & While Loops for Data Science / Data Analysis - P.3
Python For & While Loops for Data Science / Data Analysis - P.3
Luke Barousse
20 Python List Comprehension for Data Science / Data Analysis - P.4
Python List Comprehension for Data Science / Data Analysis - P.4
Luke Barousse
21 Python Functions for Data Science / Data Analysis - P.5
Python Functions for Data Science / Data Analysis - P.5
Luke Barousse
Lambda Functions for Data Science / Data Analysis - Python P.6
Lambda Functions for Data Science / Data Analysis - Python P.6
Luke Barousse
23 How NOT to learn Python for Data Science
How NOT to learn Python for Data Science
Luke Barousse
24 What is Business Intelligence (BI)? 📊😅
What is Business Intelligence (BI)? 📊😅
Luke Barousse
25 Top 3️⃣ Technical Skills for Business Intelligence 📚📊
Top 3️⃣ Technical Skills for Business Intelligence 📚📊
Luke Barousse
26 Top Non-technical Skills for Business Intelligence 📊👨🏼‍💻
Top Non-technical Skills for Business Intelligence 📊👨🏼‍💻
Luke Barousse
27 M1 vs Intel Mac for Data Science
M1 vs Intel Mac for Data Science
Luke Barousse
28 M1 vs Intel Mac for Excel 📈👨🏼‍💻
M1 vs Intel Mac for Excel 📈👨🏼‍💻
Luke Barousse
29 M1 vs Intel Mac for Python 🐍👨🏼‍💻
M1 vs Intel Mac for Python 🐍👨🏼‍💻
Luke Barousse
30 M1 vs Intel Mac for Business Intelligence Tools 💻📊
M1 vs Intel Mac for Business Intelligence Tools 💻📊
Luke Barousse
31 M1 Macbook Air vs Pro (8 vs 16 GB) for Data Science
M1 Macbook Air vs Pro (8 vs 16 GB) for Data Science
Luke Barousse
32 Python for M1 Mac vs Intel (SPOILER: M1 is 2x faster)
Python for M1 Mac vs Intel (SPOILER: M1 is 2x faster)
Luke Barousse
33 Data Analyst's WFH Setup & Upgrades
Data Analyst's WFH Setup & Upgrades
Luke Barousse
34 Windows on the M1 Mac - What are your options?
Windows on the M1 Mac - What are your options?
Luke Barousse
35 Install your favorite Windows app on M1 Mac - ft. Parallels
Install your favorite Windows app on M1 Mac - ft. Parallels
Luke Barousse
36 Data Science shortcuts for Mac
Data Science shortcuts for Mac
Luke Barousse
37 Day in the life of a data analyst
Day in the life of a data analyst
Luke Barousse
38 Power BI vs Tableau - Best BI Tool
Power BI vs Tableau - Best BI Tool
Luke Barousse
39 Mac Vs PC - BEST for Data Science
Mac Vs PC - BEST for Data Science
Luke Barousse
40 Data Scientist vs Data Analyst (funny!)
Data Scientist vs Data Analyst (funny!)
Luke Barousse
41 Become a DATA ANALYST with NO degree?!? The Google Data Analytics Professional Certificate
Become a DATA ANALYST with NO degree?!? The Google Data Analytics Professional Certificate
Luke Barousse
42 Certificates vs Degree for Data Analysts (ft. Google Data Analytics Professional Certificate)
Certificates vs Degree for Data Analysts (ft. Google Data Analytics Professional Certificate)
Luke Barousse
43 Google vs IBM Data Analyst Certificate - BEST Certificate for Data Analysts
Google vs IBM Data Analyst Certificate - BEST Certificate for Data Analysts
Luke Barousse
44 Python Vs R (funny!)
Python Vs R (funny!)
Luke Barousse
45 THIS got me my job as a Data Analyst - My portfolio tip
THIS got me my job as a Data Analyst - My portfolio tip
Luke Barousse
46 I used Python to Count my Bike Jumps!
I used Python to Count my Bike Jumps!
Luke Barousse
47 Standout as a Data Analyst with THIS TOOL
Standout as a Data Analyst with THIS TOOL
Luke Barousse
48 STOP using Spreadsheets for Everything!
STOP using Spreadsheets for Everything!
Luke Barousse
49 Transition into Data Science - My Tips & Story
Transition into Data Science - My Tips & Story
Luke Barousse
50 Get a JOB w/ Google Data Analytics Certificate?!? (ft. Certificate Holders)
Get a JOB w/ Google Data Analytics Certificate?!? (ft. Certificate Holders)
Luke Barousse
51 Staying Motivated in Data Science
Staying Motivated in Data Science
Luke Barousse
52 Data Science - Expectation vs Reality (funny!) - ft. @KenJee_ds
Data Science - Expectation vs Reality (funny!) - ft. @KenJee_ds
Luke Barousse
53 Get NOTICED in Data Science!!! (3 types of GREAT projects)
Get NOTICED in Data Science!!! (3 types of GREAT projects)
Luke Barousse
54 Use THIS to showcase EXPERIENCE in Data Science
Use THIS to showcase EXPERIENCE in Data Science
Luke Barousse
55 How to show EXPERIENCE... when you have NONE?!?
How to show EXPERIENCE... when you have NONE?!?
Luke Barousse
56 Learn PYTHON to be a DATA ANALYST?!? (or is R enough...)
Learn PYTHON to be a DATA ANALYST?!? (or is R enough...)
Luke Barousse
57 The BIGGEST MISTAKE when starting a data project!
The BIGGEST MISTAKE when starting a data project!
Luke Barousse
58 Top Jobs in Data Science
Top Jobs in Data Science
Luke Barousse
59 How to get Data Analytics side jobs - NEW LinkedIn Feature
How to get Data Analytics side jobs - NEW LinkedIn Feature
Luke Barousse
60 Building a bot to scrape job data… How NOT to collect data
Building a bot to scrape job data… How NOT to collect data
Luke Barousse

This video teaches how to use lambda functions in Python for data science and data analysis, including sorting dictionaries and creating anonymous functions. It also covers the use of filter and map functions with lambda functions.

Key Takeaways
  1. Define a lambda function to extract the second item from a tuple
  2. Use the sorted function to sort a dictionary by values
  3. Apply a function to a list of tuples to extract the second item
  4. Use lambda functions to create a concise and readable code
  5. Use lambda functions to sort dictionary items or list values
  6. Use lambda functions to return a value based on input
  7. Use lambda functions to create anonymous functions
  8. Use lambda functions with filter to get odd numbers from a list
  9. Use lambda functions with map to add a token to the end of each string in a list
💡 Lambda functions can be used to make code more concise and readable, and can be applied with other functions for more complex operations.

Related AI Lessons

Chapters (8)

Intro
0:54 Use Case: Sorting a dictionary using sorted()
3:28 Use Case: Option 1 - Utilizing a User-defined Function
5:06 Use Case: Option 2 - Utilizing a Lambda Function
6:21 Lambda Basics: Syntax and Operation
10:38 Application: Filtering a list w/ filter()
14:12 Application: Transform a list w/ map()
16:26 conclusion
Up next
Spreadsheet Guy Meets the CFO: "Define How Much"
Digital Transformation with Eric Kimberling
Watch →