Using apply() on rows with axis=1 in pandas
📖 Scenario: You work in a small bookstore. You have a table of books with their prices and quantities. You want to find the total value of each book's stock.
🎯 Goal: Create a pandas DataFrame with book data, then use apply() on rows (axis=1) to calculate the total value 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 function called
calculate_value that takes a row and returns the product of Price and Quantity.Use
apply() on books with axis=1 and the calculate_value function to create a new column TotalValue.Print the
books DataFrame to show the new column.💡 Why This Matters
🌍 Real World
Calculating total stock value helps bookstores and shops understand their inventory worth quickly.
💼 Career
Data analysts and data scientists often use apply() on rows to create new features or calculate values based on multiple columns.
Progress0 / 4 steps