Editor

Add a Computed Column

Intermediate · Write the code · from Pandas: Create, Load, and Explore DataFrames

From the lesson: Pandas: Create, Load, and Explore DataFrames

You have a spreadsheet with 10,000 rows of sales data. You need to find every order over $500, group them by region, and calculate the average. In Excel, that takes a pivot table, a filter, and a lot of clicking. In pandas, it takes three lines of code.

Read the lesson

Given a DataFrame with 'price' and 'quantity' columns, add a new column called 'total' that equals price * quantity. Then print the total column as a list using print(df['total'].tolist()).

Use the provided starter data.

import pandas as pd
df = pd.DataFrame({
'price': [10, 25, 5],
'quantity': [3, 2, 10]
})
# Add the 'total' column and print it

More Pandas DataFrame exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz