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.
-
Python Intermediate
How do I take a random sample of rows from a DataFrame in pandas?
Use sample with n for a fixed number of rows or frac for a proportion, and set random_state so the sample is reproducible. Use weights for weighted sampling and replace for sampling with replacement.
Read the answer -
Python Beginner
How do I take part of a string in every row of a pandas column?
Use the str accessor with ordinary slicing to take a fixed range of characters from every value. It is vectorised and handles missing values, unlike a loop or an apply.
Read the answer -
Python Advanced
How do I test pandas code and compare DataFrames in a test?
Use pandas.testing.assert_frame_equal, which compares values, dtypes, columns and index and reports exactly what differs. Comparing frames with == or assert does not work, because it returns a frame of booleans.
Read the answer -
Python Advanced
How do I test whether two groups differ in Python?
Use scipy.stats.ttest_ind for two independent groups, setting equal_var=False unless you know the variances match. Report the effect size alongside the p-value, because significance is not the same as importance.
Read the answer -
Python Advanced
How do I tune hyperparameters with GridSearchCV in scikit-learn?
GridSearchCV tries every combination of the parameters you list using cross-validation and keeps the best. Search over a pipeline with the step__parameter syntax, and keep a held-out test set for the final honest score.
Read the answer -
Python Intermediate
How do I turn a categorical column into indicator columns in pandas?
Use get_dummies to expand a category column into one indicator column per value. Recent pandas returns boolean columns, and drop_first avoids the redundancy that trips up some models.
Read the answer -
Python Advanced
How do I turn each group's values into proportions in pandas?
Divide each value by its group total obtained with transform, so every group's proportions sum to one. Using apply to return a whole frame works too, but transform is faster and clearer.
Read the answer -
Python Advanced
How do I turn nested JSON into a flat DataFrame in pandas?
Use pd.json_normalize, which flattens nested dictionaries into dotted column names. Use record_path and meta to expand a nested list of records while keeping the parent fields.
Read the answer -
Python Intermediate
How do I unpivot only some columns with melt in pandas?
Pass id_vars for the columns to keep as they are and value_vars for the ones to unpivot. Naming both explicitly stops melt from unpivoting columns you meant to keep.
Read the answer -
Python Intermediate
How do I use an Enum in Python and why would I?
An Enum groups related named constants into a single type, so you use meaningful names instead of loose strings or numbers. It prevents typos and invalid values, and members carry both a name and a value.
Read the answer -
Python Advanced
How do I use an SQLite database from Python?
The built-in sqlite3 module connects to a file or in-memory database, runs SQL with execute, and commits changes. Use parameter placeholders, never string formatting, to avoid SQL injection.
Read the answer -
Python Advanced
How do I use itertools to work with iterables efficiently in Python?
itertools provides lazy building blocks like chain, islice, combinations and groupby that process iterables without building large intermediate lists. They are memory-efficient and compose well.
Read the answer -
Python Advanced
How do I use logging instead of print in Python?
Use the logging module to record messages with levels like info and error, so you can control what appears and where without deleting print statements. Configure it once, and get a named logger per module.
Read the answer -
Python Advanced
How do I use regular expressions on a pandas column?
Use str.extract to pull capture groups into columns, str.contains to filter, and str.replace with regex=True to substitute. Each group in extract becomes a column, and non-matching rows become NaN.
Read the answer -
Python Intermediate
How do I use sets in Python to compare collections?
A set holds unique unhashable-free items and supports fast membership tests and comparisons like difference and intersection. Use it to find what is missing, shared, or duplicated between two collections.
Read the answer -
Python Advanced
How do I validate a DataFrame before trusting it?
Write explicit checks for the columns, types, ranges and uniqueness you expect, and fail loudly when they are violated. Catching a broken assumption at the door is far cheaper than tracing a wrong number back to it.
Read the answer -
Python Advanced
How do I validate the values in a dataclass?
Use __post_init__ to check the values after the generated constructor has run, raising if they are invalid. For heavier validation and type coercion, a library like Pydantic is a better fit.
Read the answer -
Python Advanced
How do I work with business days and skip weekends in pandas?
Use bdate_range for a range of working days and the BDay offset to add business days to a date. For real calendars you must supply the holidays yourself, because the defaults only skip weekends.
Read the answer -
Python Intermediate
How do I work with durations in pandas?
Subtracting datetimes gives a Timedelta, which you convert to a number with total_seconds or dt.days. Use total_seconds and divide, because dt.days truncates and dt.hours only gives the hour component.
Read the answer -
Python Intermediate
How do I work with file paths using pathlib in Python?
Build paths with the slash operator on Path objects, and read, write, glob and inspect files through Path methods. It is clearer and more portable than joining strings or using os.path.
Read the answer -
Python Intermediate
How do I work with text columns using the str accessor in pandas?
Use the .str accessor to run string methods on a whole column at once, such as split, contains and extract. It is vectorised, so it is faster and cleaner than looping or using apply.
Read the answer -
Python Advanced
How do I work with time zones in Python using zoneinfo?
Attach a real zone to a datetime with ZoneInfo, then convert with astimezone. zoneinfo is built into Python 3.9 and later and handles daylight saving, unlike a fixed offset.
Read the answer -
Python Advanced
How do I write a Python script that runs from the command line?
Use argparse to define arguments, put the work in a main function, and call it under the name-main guard. This gives you help text, type conversion and validation for free.
Read the answer -
Python Advanced
How do I write log messages that are actually useful later?
Log the values you will need to diagnose the problem, use the right level, and log at the boundaries of your pipeline. A message with no context is nearly worthless when something fails at 3am.
Read the answer -
Python Advanced
How do I write multiple sheets to one Excel file with pandas?
Use pd.ExcelWriter as a context manager and call to_excel once per sheet with a different sheet_name. Writing each frame to a separate file, or reopening the writer, are the mistakes that lose sheets.
Read the answer