Using apply() with lambda functions in pandas
📖 Scenario: You work in a small bookstore. You have a list of books with their prices and quantities sold. You want to calculate the total sales for each book.
🎯 Goal: Build a pandas DataFrame with book data, then use apply() with a lambda function to calculate total sales for each book.
📋 What You'll Learn
Create a pandas DataFrame called
books with columns 'Title', 'Price', and 'Quantity' using the exact data provided.Create a variable called
calculate_sales that holds a lambda function to multiply Price and Quantity.Use
apply() with the calculate_sales lambda function on the DataFrame rows to create a new column 'TotalSales'.Print the
books DataFrame to show the new 'TotalSales' column.💡 Why This Matters
🌍 Real World
Calculating total sales for products is a common task in retail and business analytics to understand revenue.
💼 Career
Data analysts and data scientists often use pandas apply() with lambda functions to quickly transform and analyze tabular data.
Progress0 / 4 steps