Q&A library

Answers, not search results

258 worked answers to the questions data analysts actually ask. Every one written from scratch, with runnable code. Describe your problem in your own words, or filter by stack.

  1. BigQuery Beginner

    How does BigQuery pricing work, and how do I avoid a surprise bill?

    On demand pricing charges for bytes scanned, not rows returned. SELECT star on a wide table can cost a hundred times more than the same query naming three columns.

    Read the answer
  2. Python Advanced

    Are list comprehensions actually faster than for loops in Python?

    Slightly, because a comprehension avoids repeated method lookups and function call overhead, but the difference is small. Neither beats a vectorised NumPy or pandas operation on numeric data, which is where the real gains are.

    Read the answer
  3. Python Intermediate

    Are Python dictionaries and sets ordered?

    Dictionaries preserve insertion order from Python 3.7 onwards, and this is guaranteed. Sets are unordered and their iteration order must never be relied upon, even when it looks stable.

    Read the answer
  4. Python Intermediate

    Do Python type hints actually do anything at runtime?

    Type hints are not enforced when the code runs, so a wrong type does not raise on its own. They are for readers and for external checkers like mypy, which catch mismatches before you run the code.

    Read the answer
  5. Python Advanced

    How do async and await work in Python?

    async def creates a coroutine, await pauses it while waiting so other tasks can run, and asyncio.run starts the event loop. It speeds up code that waits on input and output, not CPU-bound work.

    Read the answer
  6. Python Beginner

    How do I add a prefix or suffix to every column name in pandas?

    Use add_prefix or add_suffix to tag every column at once, which is useful before merging two frames with overlapping names. Use rename for selective changes instead.

    Read the answer
  7. Python Intermediate

    How do I add a row to a DataFrame in pandas?

    Build a list of rows and construct the DataFrame once, because adding rows one at a time is very slow. The old append method was removed, so use pd.concat if you must combine frames.

    Read the answer
  8. Python Advanced

    How do I add context to an exception without losing the original error?

    Use raise NewError(...) from err to attach a meaningful error while keeping the original as its cause. The traceback then shows both, which is what you need to diagnose the failure.

    Read the answer
  9. Python Intermediate

    How do I add row and column totals to a pivot table in pandas?

    Pass margins=True to pivot_table to add an All row and column holding the totals. Name it with margins_name, and use fill_value so empty cells show zero rather than NaN.

    Read the answer
  10. Python Advanced

    How do I aggregate a time series to a coarser frequency in pandas?

    Set a datetime index, then use resample with a frequency such as ME or W followed by an aggregation. resample needs a datetime index, and the frequency aliases changed in recent pandas.

    Read the answer
  11. Python Intermediate

    How do I aggregate several columns with different functions in pandas?

    Pass named aggregations to agg, giving each output column a name and the column-function pair that fills it. This produces flat, clearly named columns instead of a two-level header.

    Read the answer
  12. Python Advanced

    How do I apply a custom function over a rolling window in pandas?

    Use rolling(n).apply(func) with a function that reduces each window to a single number. It is slow because it runs Python per window, so prefer the built-in rolling methods where one exists.

    Read the answer
  13. Python Beginner

    How do I apply a function to a column in pandas?

    Use map or apply on a single column to run a function on every value, but prefer a vectorised expression or built-in string and numeric methods where one exists, because they are much faster.

    Read the answer
  14. Python Advanced

    How do I apply different preprocessing to numeric and categorical columns in scikit-learn?

    Use ColumnTransformer to route numeric columns to a scaler and categorical columns to an encoder in one step. Wrap it in a pipeline so the whole thing fits on training data only and applies consistently.

    Read the answer
  15. Python Intermediate

    How do I avoid KeyError when grouping items into a dictionary in Python?

    Use collections.defaultdict with a factory like list or int so a missing key is created automatically on first access. This removes the check-then-create boilerplate that a plain dict needs.

    Read the answer
  16. Python Intermediate

    How do I build a dictionary with a comprehension in Python?

    Use braces with a key-value expression and a for clause to build a dictionary in one line. It is clearer than a loop for simple transforms, and a common use is inverting a dictionary or filtering its items.

    Read the answer
  17. Python Intermediate

    How do I build a frequency table of two categories in pandas?

    Use crosstab to count how often each combination of two categories occurs, laid out as a grid. Add normalize for proportions and margins for row and column totals.

    Read the answer
  18. Python Advanced

    How do I build a scikit-learn Pipeline and why should I?

    A Pipeline chains preprocessing steps and a model into one object, so fit and predict apply every step in order. It prevents data leakage during cross-validation and keeps training and prediction consistent.

    Read the answer
  19. Python Intermediate

    How do I calculate a cumulative average in pandas?

    Use expanding, which grows the window from the start rather than keeping a fixed size, then apply mean or another aggregation. It answers the question of the average so far, at each point.

    Read the answer
  20. Python Advanced

    How do I calculate a running total within each group in pandas?

    Sort the data in order, then use groupby with cumsum so the running total restarts for each group. Forgetting to sort, or forgetting the groupby, are the two mistakes that give wrong totals.

    Read the answer
  21. Python Advanced

    How do I calculate a weighted average in pandas?

    Use numpy.average with a weights argument, or multiply and divide the sums yourself. A plain mean ignores the weights, which quietly gives the wrong answer when the quantities differ.

    Read the answer
  22. Python Beginner

    How do I calculate each row's percentage of the total in pandas?

    Divide the column by its own sum for a share of the overall total, or by a group total from transform for a share within each group. Multiply by 100 for a percentage.

    Read the answer
  23. Python Advanced

    How do I calculate quantiles and percentile ranks in pandas?

    Use quantile to find the value at a given percentile, and rank with pct=True to find each value's percentile position. They answer opposite questions, and the interpolation method affects the quantile result.

    Read the answer
  24. Python Intermediate

    How do I calculate the correlation between columns in pandas?

    Use corr between two columns for a single coefficient, or on the whole frame for a matrix. It measures linear association only, ignores missing values pairwise, and does not imply causation.

    Read the answer
  25. Python Advanced

    How do I calculate the time between rows in pandas?

    Subtract datetime columns or use diff to get a Timedelta, then convert to the unit you want with dt.total_seconds. Sort first and group so gaps do not span across groups.

    Read the answer