all problems

Select and filter rows

pandaseasyfiltering

The DataFrame `sales` is preloaded. Write uk_orders(df) that returns the rows where country == 'UK', keeping only the columns order_id, customer and price (in that order).

preloaded fixtures

import pandas as pd
import numpy as np

sales = pd.DataFrame({
    "order_id": [1, 2, 3, 4, 5, 6, 7, 8],
    "customer": ["ada", "grace", "ada", "alan", "grace", "marie", "alan", "ada"],
    "country": ["UK", "USA", "UK", "UK", "USA", "France", "UK", "UK"],
    "category": ["hardware", "audio", "audio", "furniture", "hardware", "audio", "hardware", "furniture"],
    "units": [1, 2, 1, 3, 1, 2, 4, 1],
    "price": [129.0, 249.99, 199.0, 420.0, 329.5, 249.99, 59.0, 540.0],
    "rating": [4.5, np.nan, 3.0, 5.0, np.nan, 4.0, 4.5, 2.5],
})

customers = pd.DataFrame({
    "customer": ["ada", "grace", "alan", "hedy"],
    "segment": ["pro", "pro", "starter", "starter"],
})

hint ladder

solution.py
loading editor…
step inspector

Inspect a chained expression (df.query(…).groupby(…).agg(…)) to see the shape and preview after each step.

output

Run your code to see its output, or check your solution to grade it.

Reference solution

The shape we check against. Any implementation that passes the assertions is valid — this one favours clarity.

def uk_orders(df):
    return df.loc[df['country'] == 'UK', ['order_id', 'customer', 'price']]

Code is blurred until you solve this problem — the reasoning stays readable.