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 Advanced

    How do I format numbers in a DataFrame for presentation in pandas?

    Use the Styler through df.style.format to control how numbers are displayed without changing the underlying values. Formatting for display and rounding the data are different things, and only one is safe.

    Read the answer
  2. Python Intermediate

    How do I generate random samples from a distribution in NumPy?

    Create a Generator with default_rng and call its distribution methods such as normal, uniform or choice. Always seed it, and use choice with p for weighted sampling.

    Read the answer
  3. Python Intermediate

    How do I get bin numbers instead of interval labels from cut in pandas?

    Pass labels=False to cut and you get integer bin numbers rather than interval objects. This is handy for modelling, but keep the boundaries somewhere, because a bare number means nothing to a reader.

    Read the answer
  4. Python Advanced

    How do I get pandas to choose better types for my columns?

    convert_dtypes moves columns to the nullable types, so integers stay integers even with missing values and text becomes a proper string dtype. It is a good tidy-up after reading messy data.

    Read the answer
  5. Python Advanced

    How do I get the previous value within each group in pandas?

    Use groupby with shift so the previous value never comes from a different group. Sort first, and expect the first row of each group to be NaN because it has no predecessor.

    Read the answer
  6. Python Intermediate

    How do I get the week number from a date in pandas?

    Use dt.isocalendar().week for the ISO week number. Be careful at year boundaries, because early January can fall in week 52 or 53 of the previous ISO year.

    Read the answer
  7. Python Intermediate

    How do I group a numeric column into ranges (bins) in pandas?

    Use pd.cut with a list of bin edges to sort numbers into labelled ranges, or pd.qcut to split them into equal-sized groups. Watch which side of each edge is included, because the default is right-closed.

    Read the answer
  8. Python Advanced

    How do I group by a time period on a date column in pandas?

    Use pd.Grouper with a key and a frequency to group a date column into periods without making it the index. This combines the convenience of resample with grouping by other columns at the same time.

    Read the answer
  9. Python Intermediate

    How do I group by more than one column in pandas?

    Pass a list of columns to groupby and the result gets a MultiIndex of the key combinations. Use as_index=False, or reset_index, to get a flat frame with the keys back as columns.

    Read the answer
  10. Python Advanced

    How do I group consecutive rows that share a value in pandas?

    Detect where the value changes, take a cumulative sum of those changes to build a run identifier, then group by it. This is how you find streaks, sessions and consecutive blocks.

    Read the answer
  11. Python Intermediate

    How do I group dates by month or quarter without losing the year in pandas?

    Use dt.to_period("M") to get a year-and-month value, so January 2021 and January 2022 stay separate. Grouping by dt.month alone merges the same month across different years.

    Read the answer
  12. Python Intermediate

    How do I handle exceptions properly with try and except in Python?

    Wrap the risky code in try and catch specific exception types, not a bare except. Keep the try block small, and use else and finally to separate success from cleanup.

    Read the answer
  13. Python Advanced

    How do I handle imbalanced classes in scikit-learn?

    Stop using accuracy, which rewards always predicting the majority class, and use a metric like F1 or ROC AUC. Set class_weight to balanced, stratify your splits, and consider resampling inside a pipeline.

    Read the answer
  14. Python Beginner

    How do I handle missing values (NaN) in pandas?

    Detect missing values with isna, remove them with dropna, or replace them with fillna. Decide deliberately whether dropping or filling is correct, because each changes your results differently.

    Read the answer
  15. Python Beginner

    How do I insert a column at a specific position in pandas?

    Use df.insert with the position, name and values, which puts the column exactly where you want rather than at the end. Reordering with a column list is the alternative for bigger rearrangements.

    Read the answer
  16. Python Intermediate

    How do I iterate over rows in a pandas DataFrame, and should I?

    You can use iterrows or itertuples, but for most work you should not loop at all. Vectorised operations on whole columns are far faster and usually clearer.

    Read the answer
  17. Python Intermediate

    How do I join text from several columns or rows in pandas?

    Use the plus operator or str.cat to combine columns row by row, and str.cat with a separator to join a whole column into one string. Missing values propagate, so fill them first.

    Read the answer
  18. Python Intermediate

    How do I join two DataFrames in pandas?

    Use merge with an on column and a how argument (left, right, inner or outer) to control which rows survive. The default is an inner join, which quietly drops non-matching rows.

    Read the answer
  19. Python Advanced

    How do I join two tables on the nearest key rather than an exact match in pandas?

    Use merge_asof to match each left row to the closest key in the right table, not an exact one. Both frames must be sorted on the key, and the default direction matches the nearest earlier value.

    Read the answer
  20. Python Advanced

    How do I keep only the groups that meet a condition in pandas?

    Use groupby.filter with a function that returns True or False for each whole group, keeping every row of the groups that pass. This is different from filtering rows, because the test applies to the group as a whole.

    Read the answer
  21. Python Intermediate

    How do I keep Python code consistently formatted and free of obvious errors?

    Use a formatter to standardise layout automatically and a linter to catch likely bugs before running. Running both in a pre-commit hook stops style debates and catches mistakes early.

    Read the answer
  22. Python Intermediate

    How do I keep tied rows when taking the top N in pandas?

    nlargest with keep="all" returns every row tied at the cut-off, rather than arbitrarily dropping one. The default silently picks whichever row came first, which can quietly change your results.

    Read the answer
  23. Python Advanced

    How do I line up two Series with different indexes in pandas?

    Use reindex to force one object onto another index, or align to bring both onto their union. Both make the missing entries explicit as NaN, rather than letting a silent mismatch produce wrong results.

    Read the answer
  24. Python Beginner

    How do I loop over a dictionary's keys and values in Python?

    Loop with .items() to get keys and values together, .keys() for keys, or .values() for values. Iterating the dictionary directly gives keys, which trips up people expecting pairs.

    Read the answer
  25. Python Beginner

    How do I loop over two lists at once in Python?

    Use zip to pair the items from two sequences and loop over the pairs. Note that zip stops at the shorter one, silently ignoring extra items, so pass strict=True to catch a length mismatch.

    Read the answer