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. Python Beginner

    How do I reverse a string in Python?

    Use the slice with a step of minus one, written as text[::-1]. It is the shortest and fastest way, and works because strings support the same slicing as lists.

    Read the answer
  2. Python Intermediate

    How do I round timestamps to the nearest hour or day in pandas?

    Use dt.floor to round down, dt.ceil to round up and dt.round to go to the nearest. Floor is usually what you want for bucketing, because round can push a timestamp into the next period.

    Read the answer
  3. Python Advanced

    How do I run a linear regression in Python and read the output?

    Use statsmodels with a formula to fit an ordinary least squares model, then read the coefficient, its p-value and the confidence interval. The coefficient is the change in the outcome per one unit of the predictor.

    Read the answer
  4. Python Advanced

    How do I run the same analysis over many files and combine the results?

    Glob the files, read each into a frame, tag it with its source, collect them in a list and concat once. Tagging the source is what lets you trace a strange row back to the file it came from.

    Read the answer
  5. Python Advanced

    How do I scale features correctly without leaking test data in scikit-learn?

    Fit the scaler on the training data only, then use transform on both training and test sets. Fitting on all the data before splitting leaks test information and inflates your scores.

    Read the answer
  6. Python Intermediate

    How do I see all the rows involved in a duplicate, not just the extra copies?

    duplicated marks only the repeats by default, hiding the first occurrence. Pass keep=False to flag every row that participates in a duplicate, which is what you want when reviewing them.

    Read the answer
  7. Python Intermediate

    How do I see custom percentiles in describe in pandas?

    Pass a percentiles list to describe to see the ones you care about, such as the 90th and 99th. The defaults hide the tail, which is exactly where the interesting behaviour usually is.

    Read the answer
  8. Python Beginner

    How do I select all the numeric or text columns in pandas?

    Use select_dtypes with include or exclude to pick columns by type, such as all numeric columns. It is more robust than listing column names by hand, which breaks when the data changes.

    Read the answer
  9. Python Advanced

    How do I select data from a MultiIndex in pandas?

    Use loc with a tuple to select across index levels, and xs to slice on an inner level by name. Sort the index first, because selecting from an unsorted MultiIndex is slow and can raise.

    Read the answer
  10. Python Advanced

    How do I select elements of a NumPy array by condition?

    Build a boolean array from a condition and use it to index, or pass a list of positions for fancy indexing. Both return a copy, and boolean masks combine with the bitwise operators, not and or.

    Read the answer
  11. Python Beginner

    How do I select rows in a DataFrame by condition?

    Build a boolean condition on a column and pass it in square brackets. Combine conditions with the and-or operators for bitwise logic, wrapping each condition in brackets.

    Read the answer
  12. Python Beginner

    How do I select rows where a value falls between two limits in pandas?

    Use between, which is clearer than writing two comparisons joined by an ampersand. It includes both endpoints by default, which is worth checking against what you mean.

    Read the answer
  13. Python Advanced

    How do I share test fixtures across several test files with pytest?

    Put shared fixtures in a conftest.py and pytest finds them automatically, with no import needed. Use the scope argument to control how often an expensive fixture is rebuilt.

    Read the answer
  14. Python Advanced

    How do I slice across levels of a MultiIndex in pandas?

    Use pd.IndexSlice to select on inner levels while leaving outer ones open, and sort the index first. Without sorting, partial slicing is slow and can raise.

    Read the answer
  15. Python Advanced

    How do I solve a system of linear equations in NumPy?

    Use np.linalg.solve, not the matrix inverse, because solve is faster and numerically more stable. Inverting a matrix to multiply by it is a common habit that quietly loses precision.

    Read the answer
  16. Python Beginner

    How do I sort a DataFrame by one or more columns in pandas?

    Use sort_values with the column name, or a list of columns for a tie-break order. Control direction per column with the ascending argument, and remember it returns a new frame unless you reassign.

    Read the answer
  17. Python Beginner

    How do I sort a list of dictionaries by a key in Python?

    Use sorted with a key function that pulls out the field to sort on, usually a lambda or itemgetter. Add reverse for descending order, and provide a fallback for missing keys.

    Read the answer
  18. Python Intermediate

    How do I split data into training and test sets in scikit-learn?

    Use train_test_split to hold back a portion of the data for testing. Set random_state for reproducibility and stratify for classification, and split before any preprocessing that learns from the data.

    Read the answer
  19. Python Intermediate

    How do I stack DataFrames together with concat in pandas?

    Use pd.concat to stack DataFrames on top of each other by rows, or side by side by columns with axis=1. It combines by position, unlike merge which combines by matching a key.

    Read the answer
  20. Python Advanced

    How do I standardise a column to z-scores in pandas?

    Subtract the mean and divide by the standard deviation to put a column on a scale of standard deviations from the mean. Decide whether to standardise per group, and remember pandas std uses the sample formula by default.

    Read the answer
  21. Python Intermediate

    How do I stop file paths breaking when someone else runs my script?

    Build paths relative to the script's own location using __file__ and pathlib, rather than relying on the current working directory. Hard-coded absolute paths break for everyone but you.

    Read the answer
  22. Python Advanced

    How do I stop forward fill carrying a value too far in pandas?

    Pass limit to ffill so it only fills a set number of consecutive gaps, and group so a value never carries across into another group. An unlimited fill can invent long stretches of data.

    Read the answer
  23. Python Beginner

    How do I stop pandas truncating my DataFrame output with dots?

    Pandas truncates wide or long output for readability. Raise display.max_columns and max_rows to see more, ideally inside a context manager so the change does not leak.

    Read the answer
  24. Python Advanced

    How do I structure a Python project so my imports work?

    Put your code in a package folder, add a pyproject.toml, and install it in editable mode with pip install -e. This makes imports work from anywhere instead of depending on which directory you ran from.

    Read the answer
  25. Python Beginner

    How do I summarise values by group in pandas?

    Use groupby with an aggregation such as sum or mean, then reset_index if you want the grouping column back as an ordinary column. Use agg to produce several named summaries at once.

    Read the answer