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 Advanced
Why is adding a day to a datetime wrong across a daylight saving change?
Adding a timedelta to an aware datetime keeps the wall clock time, so across a clock change only 23 or 25 hours actually pass. Convert to UTC, add the duration, then convert back if you mean a true elapsed day.
Read the answer -
Python Advanced
Why is apply with axis=1 so slow in pandas, and what should I use instead?
apply with axis=1 runs a Python function once per row, so it is orders of magnitude slower than a vectorised expression. Replace it with column arithmetic, np.where or np.select wherever the logic allows.
Read the answer -
Python Intermediate
Why is my matplotlib plot not showing?
In a script you usually need plt.show() to display a figure, and it blocks until you close the window. In notebooks the cause is different, and a non-interactive backend shows nothing on screen at all.
Read the answer -
Python Advanced
Why is my model's feature importance misleading?
Tree impurity importance is biased towards high-cardinality features and is computed on training data. Use permutation importance on held-out data, and remember importance is not causation.
Read the answer -
Python Advanced
Why is my Python loop so slow when checking if an item is in a list?
Checking membership in a list scans it from the start, so doing that inside a loop makes the work grow with the square of the size. Convert the list to a set first and each check becomes effectively instant.
Read the answer -
SQL Intermediate
How do I rank rows within a group using a window function?
Use RANK, DENSE_RANK or ROW_NUMBER with PARTITION BY. The three differ only in how they treat ties, and choosing the wrong one silently changes your answer.
Read the answer -
Statistics Beginner
What does a p value actually mean, and what does it not mean?
It is the probability of seeing data at least this extreme if the null hypothesis were true. It is not the probability that the null hypothesis is true, and it never was.
Read the answer -
Survey design Intermediate
How do I build validation into an ODK or Kobo form so errors are caught at entry?
Use constraint and required in the XLSForm. A value rejected on the enumerator's phone costs seconds. The same value discovered during cleaning costs a callback, or a wrong number in a report.
Read the answer