all problems

Filter SF trips

pandaseasyfiltering

The DataFrame `trips` is preloaded. Write sf_trips(df) that returns trips where city == 'SF', keeping only trip_id, driver, and fare columns (in that order).

preloaded fixtures

import pandas as pd
import numpy as np

trips = pd.DataFrame({
    "trip_id": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    "driver": ["alex", "alex", "sam", "sam", "alex", "jordan", "jordan", "sam", "alex", "jordan"],
    "city": ["SF", "SF", "NYC", "NYC", "SF", "LA", "LA", "NYC", "SF", "LA"],
    "fare": [12.5, 8.0, 20.0, 15.0, 9.5, 18.0, 22.0, 11.0, 14.0, 16.5],
    "surge": [1.0, 1.0, 1.5, 1.2, 1.0, 2.0, 1.5, 1.0, 1.3, 1.8],
    "distance": [3.2, 2.1, 5.5, 4.0, 2.8, 6.0, 7.2, 3.5, 3.8, 5.0],
})

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 sf_trips(df):
    return df.loc[df['city'] == 'SF', ['trip_id', 'driver', 'fare']]

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