How does a lambda function work in Pandas?

How does a lambda function work in Pandas

Recipe Objective

Suppose we wish to perform any operation on row or column of dataset. Now we can do it through a loop or set up a lambda function for the same.

So this recipe is a short example on How does a lambda function work in Pandas. Let's get started.

Step 1 - Import the library

import pandas as pd import seaborn as sb

Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays. Seaborn will help us in importing dataset.

Step 2 - Setup the Data

df = sb.load_dataset('tips') print(df.head())

Here we have imported datset from seaborn library.

Now, our dataset is ready.

Step 3 - Applying lambda function

df = df.assign(Percentage = lambda x: (x['tip'] /x['total_bill'] * 100)) print(df.head())

Now we are creating a newcolumn of percentage as tip vs total bill.

Step 4 - Let's look at our dataset now

Once we run the above code snippet, we will see:

Scroll down the ipython file to visualize the final output.

We can see a new column of Percentage being created with the formula as mentioned above.

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Build a Refund Request AI Agent using LangGraph and FAISS
In this AI Project, you will learn to build an intelligent, multi-agent refund management system that automates validation of customer requests, enforces business policies through semantic search, and provides consistent customer communication.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Build an Outreach AI Agent using CrewAI,Twilio and OpenAI APIs
In this project, you will learn to build an end-to-end AI-powered customer outreach system using CrewAI. You’ll design a workflow where different AI agents handle different tasks like analyzing customer data, creating personalized call scripts, making voice calls, and sending follow-up emails.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.