Map( ), Apply( ), Applymap( ) With the Lambda FunctionEvelyn LiBlockedUnblockFollowFollowingMar 25IntroductionFor beginners in Data Science, there are just way too many things we need to learn.
First the Terminal, then the Jupyter Notebook, the Pandas, and more to come.
As I list out all these things, you might notice that I myself am also a beginner in this long journey that we are about to launch ourselves into.
Although the path might seem to be too long and we all need more patience than we have, things will eventually turn around.
One day, one of those functions in Pandas that you had no idea what it meant, and you just use it because other people said so, will eventually start to make sense.
For this reason, I sincerely hope this blog post will be useful to one of you out there in understanding a little more about how to use map( ), apply( ), applymap( ) with lambda functions as they come in handy when working on large data sets.
The Lambda FunctionWhat is a Lambda FunctionIn Python, writing a normal function start with defining the function with the def keyword.
Meanwhile, lambda functions, also known as an anonymous function, start with the keyword lambda.
The syntax of Lambda Functionlambda arguments : expressionIn other words, lambda functions are a short version of the normal function that takes one expression and needs no return statement at the end.
Here is an example where a lambda function will be very useful.
Let’s say, we want a multiple of two cells, instead of doing it manually, we can just use a lambda function.
So when is the best time to use the Lambda Function?From my limited interaction with python and pandas, I think of lambda functions the most when working with lists, series or data frames in python and pandas; especially when there is an operation or transformation that needs to happen to a pandas series or data frame.
With the combination of map(), apply(), or applymap(), lambda function can be quite handy.
I will provide some examples in the following section.
Before that, I would like to talk about the difference between map(), apply(), and applymap().
Map ( )The map() method only works on panda series where different types of operation can be applied to the items in the series.
how does map() function workWhen you apply the map(function) method on a series, the map() function takes each element in the series and applies the function to it, and returns the transformed series.
Here is an example:Apply ( )The apply () method works on panda series and data frames with a variety of functions easily applied depending on the datatype.
how does apply() function workSimilar to map(), when you use the apply() method on a series or a data frame, the function takes each element in the series and apply the function onto the element, then returns the transformed series or data frame.
Here is an example:Well, of course, it does not make sense if we only change two columns in the dataset from cm to mm.
The next question is, what if we want to change the entire data frame?.There are two ways we can do this.
And it works!!.But, there is just way too much typing for me with all the variable names and names and names and names… And what if there are 15 columns in your data frame, or 50… OR 500!!!Let me introduce you to applymap( ), our lifesaver today!!Applymap ( )The applymap() method works on the entire pandas data frame where the input function is applied to every element individually.
In other words, applymap() is appy() + map()!Here is an example!Combining Lambda Function With Apply( )In the previous sections, we talked about the lambda function as well as map(), apply(), and applymap() individually.
Now, let's combine them and see why they work so well with one another.
MAGIC TIME!!!If you go back to the beginning of the post, you will notice that I wrote a function cm_to_mmto perform the transformation in each column.
Here is a quick reminder of the function:def cm_to_mm(cm): mm = cm * 10 return mmAs you can see, this function is not that complicated, all we did was take a number, and then multiply the number by 10.
This function can be easily transformed into a lambda function.
lambda x: x*10First, we define this function with the keyword, and then the variable x is our argument followed by : just like any normal function we define.
The part after the colon :is the expression where it takes x and multiplies it by 10.
Remember, the lambda function only takes one expression.
You can feed it only one input, and it will return one output.
You might wonder why I would say lambda functions are useful at all because it only takes one thing and returns one thing, but remember, this feature fits perfectly with map(), apply(), and applymap() where they take the function and apply it to every element in a series or data frame individually, then return the transformed output.
I always imagine it like this:assembly GIF on GIFEREdit descriptioni.
comThe series/data frame: line of bearEach element in the series: one bearInput: one bearFunction: red sweaterOutput: bear with sweaterLet’s also use an actual example to show the process.
More ExamplesOf course, the example above is only one of the ways to use apply(), map(), applymap() functions.
In the following section, I will show you some cool ways to use apply-lambda combination.
Example 1, Create a new column with existing columnsThe apply function will allow us to pass a function over an axis of our DataFrame:here, we specify axis=1, which means that we are grabbing all of our rows and applying our function to their columns.
lambda functions are disposable, and often just use the variable x as a placeholder for whatever they're operating on:because we’re operating on our rows, x becomes a row each time our function is applied.
we can specify which columns we want to operate on, noting that those column values will be multiplied by the same column in that row.
Example 2, apply- lambda with strings extractionReferencesI would like to provide credit reference to the following websites and people who have helped me in putting my thoughts and words together.
Also, within this notebook, I referenced their work and example for the purpose of better explaining concepts.
Here are the relevant links:https://medium.
org/blog/python-lambda-functionsAlso, I would like to thank General Assembly and my dear local instructor Mark and global instructor Dave.
Without their help, I will not be able to understand any of these.