$ python interview.py

Pandas Interview Questions and Answers

6 questions in the style data and analytics interviews actually ask, from simple filters to groupby and merges. Each one has the answer, a plain-English explanation and the most common mistake. Try it yourself first — every question opens in a live Python editor with pandas in your browser.

1. Select and filter rows

Easy · Boolean filtering with .loc

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).

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

Why it works: df['country'] == 'UK' builds a True/False mask, one value per row. .loc[mask, columns] keeps the True rows and the listed columns in the order you give them.

Common mistake: Chaining df[mask][cols] then assigning to it — that can trigger SettingWithCopyWarning. Use a single .loc call.

run it in the editor

2. Create a revenue column

Easy · assign() without mutating

Write add_revenue(df) that returns a copy of df with a new column `revenue` = units * price. Do not mutate the input.

show answer
def add_revenue(df):
    return df.assign(revenue=df['units'] * df['price'])

Why it works: Column maths in pandas is vectorised: units * price multiplies row by row. assign() returns a new DataFrame, so the original stays untouched.

Common mistake: Writing df['revenue'] = ... inside the function, which silently changes the caller's DataFrame.

run it in the editor

3. Group by and aggregate

Easy · groupby + sum + sort

Write revenue_by_category(df) returning a Series indexed by category with the total revenue (units * price), sorted descending.

show answer
def revenue_by_category(df):
    rev = df['units'] * df['price']
    return rev.groupby(df['category']).sum().sort_values(ascending=False)

Why it works: Group a computed Series by another column, sum each group, then sort_values(ascending=False) to put the biggest group first. The result is a Series indexed by category.

Common mistake: Forgetting to sort, or calling reset_index() when the question asks for a Series indexed by category.

run it in the editor

4. Handle missing ratings

Easy · fillna with the mean

Write fill_ratings(df) returning a copy where missing `rating` values are replaced by the mean rating (rounded to 2 decimals).

show answer
def fill_ratings(df):
    out = df.copy()
    out['rating'] = out['rating'].fillna(round(out['rating'].mean(), 2))
    return out

Why it works: mean() skips missing values automatically, so it's safe to compute before filling. Work on a copy so the input keeps its gaps.

Common mistake: Using dropna(), which deletes rows instead of filling them.

run it in the editor

5. Multi-column aggregation

Medium · Named aggregation

Write customer_summary(df) returning a DataFrame indexed by customer with columns `orders` (row count) and `units` (total units), sorted by orders descending then customer ascending.

show answer
def customer_summary(df):
    out = df.groupby('customer').agg(orders=('order_id', 'count'), units=('units', 'sum'))
    return out.sort_values(['orders', 'customer'], ascending=[False, True])

Why it works: agg(orders=('order_id', 'count'), units=('units', 'sum')) builds several output columns in one pass with the names you choose. sort_values with two keys and two directions breaks ties.

Common mistake: Passing a single ascending=False, which sorts the tie-breaker the wrong way.

run it in the editor

6. Merge two DataFrames

Medium · Left merge

`customers` is also preloaded. Write with_segment(df, customers) that left-joins segment onto the sales rows on `customer`, keeping every sales row.

show answer
def with_segment(df, customers):
    return df.merge(customers, on='customer', how='left')

Why it works: merge(..., on='customer', how='left') keeps every row from the left table and fills missing matches with NaN — the pandas version of SQL's LEFT JOIN.

Common mistake: Using the default how='inner', which drops sales rows for customers with no segment.

run it in the editor

Ready for harder questions?

Take the 3-minute diagnostic to find your weak spots, or browse company-style questions.