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 define and use a custom exception in Python?
Subclass Exception to create a named error your callers can catch specifically. A custom exception makes failures self-describing and lets callers handle your errors without catching everything.
Read the answer -
Python Advanced
How do I detect outliers in a column using the IQR method in pandas?
Compute the interquartile range, then flag values more than 1.5 times the IQR below the first quartile or above the third. This is robust to the outliers themselves, unlike a mean-and-standard-deviation rule.
Read the answer -
Python Beginner
How do I drop columns or rows in pandas?
Use drop with the columns argument to remove columns and the index argument to remove rows. It returns a new frame, so reassign, and errors="ignore" stops it raising on names that are not there.
Read the answer -
Python Advanced
How do I drop rows or columns that are mostly empty in pandas?
Use dropna with thresh to require a minimum number of real values, or how="all" to drop only entirely empty rows. To drop sparse columns, filter on the proportion of missing values instead.
Read the answer -
Python Advanced
How do I evaluate a model with cross-validation in scikit-learn?
Use cross_val_score to train and test on several splits and average the results, giving a more reliable score than a single split. Pass a pipeline so preprocessing is refitted per fold, and stratify for classification.
Read the answer -
Python Advanced
How do I fill a column from a second column where the first is missing in pandas?
Use fillna with another Series, or combine_first, to take the first column's value where present and fall back to the second where missing. This is the pandas equivalent of SQL COALESCE.
Read the answer -
Python Advanced
How do I fill gaps in a series by interpolating in pandas?
Use interpolate to estimate missing values from the ones around them, linearly by default. It suits ordered numeric data like time series, and you should sort first and consider limiting how far it fills.
Read the answer -
Python Advanced
How do I fill missing values using each group's own values in pandas?
Combine groupby with transform to fill each group's gaps from that group's own mean or neighbours, rather than from the whole column. A group that is entirely missing cannot fill itself, so those gaps remain.
Read the answer -
Python Intermediate
How do I filter rows matching any of several text patterns in pandas?
Join the patterns with the regex or symbol inside str.contains, or build the pattern from a list. Set na=False so missing values do not raise, and escape special characters when matching literally.
Read the answer -
Python Intermediate
How do I find and remove duplicate rows in pandas?
Use duplicated to flag repeats and drop_duplicates to remove them. Control which columns define a duplicate with subset, and which copy to keep with the keep argument.
Read the answer -
Python Advanced
How do I find rows in one DataFrame that are not in another in pandas?
For a single key, filter with isin and negate it. For multiple keys or to keep the right-hand columns, use a merge with indicator and keep the left_only rows. This is called an anti-join.
Read the answer -
Python Advanced
How do I find the differences between two DataFrames?
Use equals for a quick same-or-not check and compare to see exactly which cells differ. For frames with different rows, merge with an indicator to find what is missing from each side.
Read the answer -
Python Beginner
How do I find the most common value in a pandas column?
Use mode, which returns every value tied for most frequent, or value_counts().idxmax() for a single answer. mode returns a Series precisely because there can be more than one winner.
Read the answer -
Python Intermediate
How do I find the row with the maximum value in pandas?
max gives the value, idxmax gives the index label of the row holding it, so pass idxmax to loc to get the whole row. Combine with groupby to get the top row within each group.
Read the answer -
Python Advanced
How do I find what is using memory in my Python program?
Use tracemalloc from the standard library to see which lines allocated the most memory. Measure rather than guess, because the biggest consumer is often not the object you suspect.
Read the answer -
Python Beginner
How do I find where the smallest value is in pandas?
idxmin gives the index label of the smallest value, while argmin gives its integer position. Use idxmin with loc to fetch the whole row, and be aware both silently pick the first of any ties.
Read the answer -
Python Advanced
How do I fix a UnicodeDecodeError when reading a file?
The file is not in the encoding you assumed, usually UTF-8. Find the real encoding and pass it, but never fall back to latin-1 blindly, because it decodes anything and silently produces mojibake.
Read the answer -
Python Intermediate
How do I fix some arguments of a function in Python?
Use functools.partial to build a new function with some arguments already supplied. It is clearer than a lambda for this purpose and keeps the original function reusable.
Read the answer -
Python Intermediate
How do I fix the SettingWithCopyWarning in pandas?
The warning means pandas cannot tell whether you are writing to the original frame or a temporary copy. Do the whole operation in one loc call, and note that pandas 3.0 replaces this warning with a ChainedAssignmentError under Copy-on-Write.
Read the answer -
Python Intermediate
How do I flag rows where a value changed in pandas?
Compare the column with itself shifted by one row, grouping so the comparison stays within each group. The first row of each group compares against NaN and is flagged as changed, which is usually correct.
Read the answer -
Python Beginner
How do I flatten a list of lists in Python?
Use a nested list comprehension for one level of nesting, which is both clear and fast. For arbitrary depth you need recursion, and for numeric grids reach for numpy instead.
Read the answer -
Python Advanced
How do I flatten the column names after a groupby with multiple aggregations in pandas?
A groupby with several aggregations produces two-level column names. Flatten them by joining the levels into single strings, or avoid them entirely with named aggregation.
Read the answer -
Python Advanced
How do I format dates on the x-axis of a matplotlib chart?
Use matplotlib.dates locators and formatters to control the tick spacing and label format, and pass real datetime values, not strings. Rotate crowded labels or use autofmt_xdate to stop them overlapping.
Read the answer -
Python Beginner
How do I format numbers and text with f-strings in Python?
Put an f before the quote and drop expressions in braces. A colon after the expression adds a format spec, such as .2f for two decimals or a comma for thousands separators.
Read the answer -
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