Calculate a whole column
A spreadsheet formula is often copied down the sheet. In pandas, arithmetic between columns already works row by row:
invoice_lines["gross"] = invoice_lines["quantity"] * invoice_lines["unit_price"] invoice_lines["net"] = invoice_lines["gross"] - invoice_lines["discount"]
Assigning to a new label adds a column; it does not change the original columns. Parentheses keep units clear: (minutes / 60) * hourly_rate converts minutes into paid hours before multiplying.
Percentages need a scale. A value of 15 in a discount_pct column means 15 / 100, not 15 times the price. Keep full precision for calculations; use round(2) only when the task asks for rounded money.
See it run
import pandas as pd cart = pd.DataFrame({"item": ["pen", "pad", "ink"], "qty": [3, 2, 1], "price": [2.5, 4.0, 9.0]}) cart["total"] = cart["qty"] * cart["price"] cart["pricey"] = cart["total"] > 7.5
One line of math applies to every row. No loop needed.
Practice calculate a whole column
Exercise 1
You prepare a print-shop invoice. Read print_lines.csv into invoice_lines. Add gross = quantity * unit_price and net = gross - discount, where discount is a fixed credit amount per line, not a percentage. Keep every original row and column.
Exercise 2
A field-service planner needs labor estimates. Load callouts.csv into jobs. Add hours = minutes / 60 and labor_cost = hours * hourly_rate. Do not truncate partial hours or round intermediate values. Keep the source fields.
Exercise 3
A stationery buyer compares supplier offers. Read supplier_offers.csv as offers. Add net_unit = list_price * (1 - discount_pct / 100), then order_cost = net_unit * units. Keep unrounded values and all source fields. A discount of 20 means twenty percent off.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.