DataLane
← All cheat sheets

pandas Interview Questions cheat sheet

Indexing, copies, groupby, merges, dtypes, and the pandas performance questions that still show up in data interviews.

Interview PrepIntermediate6 sections

Indexing and copies

What is the difference between loc, iloc, and []?
loc is label-based, iloc is position-based, and [] is a convenience that switches meaning with the input type. [] returning a copy or a view is version- and dtype-dependent, which is why SettingWithCopyWarning exists. In production code I use loc for assignment and treat [] as read-only sugar.
What does SettingWithCopyWarning actually mean?
You assigned into something pandas cannot prove is a view of the original, so the write may vanish. The fix is to assign with loc on the original frame, or explicitly .copy() and then assign. Chained indexing like df[df.a > 0]['b'] = 1 is the canonical trigger.
When does pandas give you a view versus a copy?
Single-dtype (NumPy-backed) slices can be views; multi-dtype frames, boolean masks, and most method chains return copies. Copy-on-Write in pandas 3 makes the semantics consistent: reads can share memory, writes copy. Interviewers still ask the old question, so mention CoW and the historical mess.
Why is iterrows considered a mistake?
It is a Python-level row loop with dtype reconstruction each row — often 100x slower than a vectorized operation. Use vectorized arithmetic, .to_numpy() plus a ufunc, or itertuples if you truly must loop. apply is only better than iterrows, not actually fast.

Types and missing data

Why did my integer column become float64?
A single NaN upcasts the whole column because classic NumPy integers cannot hold NA. Use nullable dtypes (Int64, boolean) or pyarrow-backed dtypes so missing values do not change the type. Silent upcast is how a join key stops matching.
How do you handle missing values without destroying a join key?
Do not fillna on the key. Drop or quarantine rows with a null key, and fill measures separately. fillna(0) on a key invents a customer zero that then groups together. Interviewers watch whether you treat keys and measures the same.
What is the difference between None, NaN, NaT, and pd.NA?
None is Python's null and becomes NaN in float columns. NaN is IEEE float. NaT is the datetime equivalent. pd.NA is the pandas-native missing sentinel used by nullable dtypes. Mixing them in one column is how equality checks surprise you.
When do you convert to categorical?
Low-cardinality string columns you group or join on, especially before a merge. Memory drops and groupby can get faster. Do not categorical-encode a high-cardinality id; you pay for a giant mapping and gain nothing. Arrow-backed strings often beat object strings without going categorical.

Combining frames

merge versus join versus concat?
merge is the SQL-style join on columns. join joins on the index. concat stacks along an axis. Most pipeline bugs are a merge with the wrong how or a non-unique key. I write merge explicitly and almost never use join, because index-based joins hide the key.
How do you detect a fan-out merge in pandas?
Compare len(result) to len(left). If it grew and you expected a many-to-one, the right key was not unique. validate='m:1' on merge raises instead of silently multiplying rows. This argument is the most underused pandas feature in interviews.
When does concat along axis=1 produce misaligned data?
When indexes do not match. concat aligns on the index, so two frames that 'look' row-aligned in a notebook can shift. Reset the index or merge on a real key. Relying on positional alignment after a filter is a classic silent bug.
How do you upsert in pandas?
There is no native MERGE. Typical pattern: set_index on the key, combine_first or a concat plus drop_duplicates(subset=key, keep='last'). At any serious volume, do the upsert in the warehouse instead. pandas is the wrong upsert engine.

Groupby and windows

What does as_index=False do, and when do you need it?
Leaves the group keys as columns instead of an index, which is what you want before writing back to SQL. Forgetting it is why people call reset_index after every groupby. In recent pandas, named aggregations plus as_index=False is the readable form.
transform versus apply versus agg?
agg reduces to one row per group. transform returns a same-shaped series aligned to the original index — the way you compute a group mean to then filter outliers. apply is the generic escape hatch and the slow path. Prefer agg and transform; reach for apply last.
How do you compute a rolling metric per customer?
Sort by customer and time, then groupby('customer')['amount'].rolling(7).sum(). The sort is not optional; rolling is positional. Missing the sort is how a rolling 7 becomes seven arbitrary rows.
Why is groupby.apply slow, and what do you replace it with?
It runs a Python function per group. Replace with named aggregations, transform, or a vectorized expression. If the logic truly cannot vectorize, consider polars or a warehouse. 'I used apply' is not a performance strategy.

Performance

How do you reduce memory of a 5 GB CSV in pandas?
Read only needed columns, set dtypes on read (especially categoricals and Int32), and use chunksize or pyarrow as the engine. Downcast after the fact is plan B. If it still does not fit, you should not be in pandas — use polars scan, DuckDB, or the warehouse.
Why is reading CSV with the default engine painful?
The Python engine is slow and type-infers from a sample, so a later alphanumeric id blows up or silently becomes object. engine='pyarrow' and explicit dtypes fix both. For anything you will read more than once, convert to Parquet and stop paying the CSV tax.
When do you switch from pandas to polars or DuckDB?
When the working set no longer fits comfortably in memory, when compile-time expressions would replace a chain of apply, or when you want a lazy scan over Parquet. pandas remains right for small, interactive, and library-ecosystem reasons. Loyalty to pandas past a few tens of GB is not a virtue.
What does copy-on-write change about performance advice?
Chained reads no longer defensively copy, so some old 'avoid chaining' advice is outdated. Writes copy, so mutating in a loop is still bad. The durable advice: prefer expressions that produce a new frame over in-place mutation, and measure with memory_profiler rather than folklore.

Production habits

How do you make a pandas transform testable?
A pure function from DataFrame to DataFrame, with a fixture of 5 to 20 rows covering the grain, a null key, and a duplicate. Assert on row count and one computed column. A notebook you 'just run' is not a test.
What belongs in pandas versus in SQL in a pipeline?
Joins, filters, and aggregations over warehouse-scale data belong in SQL. pandas is for the awkward bits: an API response, a statistical function the warehouse lacks, or a small enrichment. Doing the warehouse's job in pandas is how jobs OOM at 4 a.m.
How do you version a pandas-dependent pipeline?
Pin pandas and numpy. pandas 2 to 3 is a CoW and dtype migration. Tests against a golden output Parquet catch the silent behavior changes. Unpinned pandas in production is a time bomb, not flexibility.
What is a pandas answer that would worry you in an interview?
inplace=True as a default, iterrows in a hot path, merge without talking about key uniqueness, and fillna on a join key. Those four signal someone who has used pandas without operating a pipeline.

From DataLane — tutorials at/blog, practice SQL live in theplayground.

↑↓ navigate openesc close