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 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