17. Pandas Interview Questions (50+)
1. Series vs DataFrame? 1-D labeled array vs 2-D labeled table.
1. Series vs DataFrame? 1-D labeled array vs 2-D labeled table.
2. loc vs iloc? Labels (end-inclusive) vs integer positions (end-exclusive).
3. at vs loc? at is a fast scalar accessor for a single cell by label; loc handles slices/masks.
4. How to filter rows? Boolean mask df[df.col > x] or df.query('col > x').
5. Why &/| not and/or? Element-wise operations on boolean Series; wrap conditions in parentheses.
6. How to handle missing data? isna, dropna, fillna (mean/median/ffill/bfill), or to_numeric(errors='coerce').
7. dropna how options? how='any' (default) drops if any NaN; how='all' drops only if all NaN.
8. fillna methods? Constant value, ffill (forward), bfill (backward), or a computed stat.
9. How to remove duplicates? df.drop_duplicates(subset=, keep='first'/'last'/False).
10. apply vs map vs applymap? Axis-wise/element vs Series element-wise vs DataFrame element-wise.
11. What is split-apply-combine? The groupby paradigm: split into groups, apply a function, combine results.
12. transform vs agg? agg reduces group to one row; transform returns a value per row.
13. Named aggregation syntax? df.groupby('k').agg(new=('col','func')).
14. merge vs join? merge on columns/keys; join on index by default.
15. merge vs concat? Key-based combine vs stacking along an axis.
16. Join types? inner, left, right, outer, cross.
17. How to detect a many-to-many merge? Check key uniqueness or use validate= argument.
18. pivot vs pivot_table? pivot reshapes only (fails on dup keys); pivot_table aggregates.
19. melt purpose? Unpivot wide → long format.
20. stack vs unstack? Move columns↔index levels.
21. What is a MultiIndex? Hierarchical index of multiple levels.
22. Why sort a MultiIndex? Slicing requires a lexsorted index.
23. How to reset an index? df.reset_index() moves index into columns.
24. How to set an index? df.set_index('col').
25. value_counts vs groupby.size? value_counts counts a single Series; groupby.size counts rows per group.
26. Difference size and count in groupby? size includes NaN; count excludes NaN.
27. How to rename columns? df.rename(columns={...}) or assign df.columns=[...].
28. How to change a column dtype? df['c'].astype(...) or pd.to_numeric/to_datetime.
29. How to parse dates? pd.to_datetime or parse_dates in read_csv.
30. How to extract year/month? df['d'].dt.year, .dt.month.
31. What is resampling? Changing time-series frequency (e.g. daily→monthly) with aggregation.
32. Rolling vs expanding vs ewm? Fixed window / cumulative / exponentially weighted.
33. How to compute a moving average? df['x'].rolling(7).mean().
34. How to select by regex on strings? df['c'].str.contains(pat, regex=True).
35. How to split a column into two? df['c'].str.split('-', expand=True).
36. How to reduce memory? Downcast numerics, category dtype, usecols, drop columns.
37. Why is iterrows slow? It builds a Python Series per row; vectorize instead.
38. What is query? String-expression filtering: df.query('a > 1 and b < 5').
39. What does inplace=True do — should you use it? Modifies in place; discouraged (poor chaining, being deprecated). Prefer reassignment.
40. copy vs view / SettingWithCopyWarning? Chained indexing may return a view; assign via .loc to avoid ambiguous writes and the warning.
41. How to add a column? df['new'] = ... or df.assign(new=...).
42. How to drop a column? df.drop(columns=['c']).
43. How to combine categories? map/replace with a dict, or pd.cut for binning.
44. What is pd.cut vs pd.qcut? cut = fixed bin edges; qcut = equal-frequency (quantile) bins.
45. How to one-hot encode? pd.get_dummies(df['col']).
46. How to sort by multiple columns? df.sort_values(['a','b'], ascending=[True,False]).
47. How to get top-N per group? df.groupby('g').apply(lambda x: x.nlargest(n,'v')) or groupby(...).head(n) after sorting.
48. How to compute a cumulative sum? df['c'].cumsum() or groupby('g')['c'].cumsum().
49. How to shift/lag a column? df['c'].shift(1) — great for period-over-period change.
50. How to compute % change? df['c'].pct_change().
51. How to merge on different column names? pd.merge(l, r, left_on='a', right_on='b').
52. How to concatenate columns of strings? df['a'] + '_' + df['b'] or df[['a','b']].agg('_'.join, axis=1).
53. How to find rows in A not in B? merge with indicator=True, keep left_only; or ~df.a.isin(other.a).
54. How to sample data? df.sample(n=, frac=, random_state=).
55. How to explode a list column into rows? df.explode('col').