What is Lambda Function and it's application
Lambda Function
We can do this with the apply() function in Pandas. apply() function calls the lambda function and applies it to every row or column of the dataframe and returns a modified copy of the dataframe:
![lambda function with apply](https://cdn.analyticsvidhya.com/wp-content/uploads/2020/02/lambda5.png)
Introduction
A lambda function is a small function containing a single expression. Lambda functions can also act as anonymous functions where they don’t require any name. These are very helpful when we have to perform small tasks with less code.
We can also use lambda functions when we have to pass a small function to another function. Don’t worry – we’ll cover this in detail soon when we see how to use lambda functions in Python.
Lambda functions are handy and used in many programming languages but we’ll be focusing on using them in Python here. In Python, lambda functions have the following syntax:
Lambda functions consist of three parts:
- Keyword
- Bound variable/argument, and
- Body or expression
Comparing Lambda with regular function
lambda x: x+1 v/s def add1(x):
return(x+1)
Lambda functions are defined using the keyword lambda. They can have any number of arguments but only one expression. A lambda function cannot contain any statements, and it returns a function object which can be assigned to any variable. They are generally used for one-line expressions.
Regular functions are created using the def keyword. They can have any number of arguments and any number of expressions. They can contain any statements and are generally used for large blocks of code.
Application of Lambda
df=pd.DataFrame({ 'id':[1,2,3,4,5], 'name':['Jeremy','Frank','Janet','Ryan','Mary'], 'age':[20,25,15,10,30], 'income':[4000,7000,200,0,10000] })
![lambda dataframe](https://cdn.analyticsvidhya.com/wp-content/uploads/2020/02/lambda3.png)
Lambda with apply() function
df['age']=df['age'].apply(lambda x: x+3)
![lambda function with apply](https://cdn.analyticsvidhya.com/wp-content/uploads/2020/02/lambda5.png)
Lambda functions are quite useful when you’re working with a lot of iterative code. They do appear complex, I understand, but I’m sure you’ll have grasped their importance in this tutorial.
I am working with a bank and want to make career in data science . But i have not any degree of data science. Can i do any short term courses and make a career in data science please suggest
ReplyDeleteOf-course you can do any diploma course or online course to learn about data science.
Delete