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
What is the difference between an iterable and an iterator in Python?
An iterable can produce an iterator, which is the thing that actually yields items one at a time and gets used up. A list can be looped many times, an iterator only once.
Read the answer -
Python Intermediate
What is the difference between a NumPy array and a Python list?
A NumPy array holds one fixed type in a compact block of memory and supports fast whole-array maths, while a list holds any mix of objects and grows flexibly. Use arrays for numeric computation and lists for general collections.
Read the answer -
Python Beginner
What is the difference between append and extend on a Python list?
append adds its argument as a single element, so passing a list nests it. extend adds each item of an iterable individually. Both change the list in place and return None.
Read the answer -
Python Intermediate
What is the difference between a shallow copy and a deep copy in Python?
A shallow copy duplicates the outer container but shares the nested objects, so changes to a nested item show in both. A deep copy duplicates everything recursively, giving full independence.
Read the answer -
Python Intermediate
What is the difference between at, iat, loc and iloc in pandas?
at and iat fetch a single cell and are faster than loc and iloc, which handle ranges and slices. Use at for a label lookup of one value and iat for a positional one.
Read the answer -
Python Beginner
What is the difference between a tuple and a list in Python?
A list is mutable and meant for a collection of similar items you may change. A tuple is immutable and often used for a fixed record of related values. Immutability lets tuples be dictionary keys.
Read the answer -
Python Advanced
What is the difference between first and head in a pandas groupby?
first returns the first non-missing value in each column, skipping NaN, while head returns the first row as it is, missing values included. They differ exactly when the first row has gaps.
Read the answer -
Python Beginner
What is the difference between is and == in Python?
== checks whether two values are equal, while is checks whether they are the exact same object in memory. Use == for value comparisons and reserve is for checks against None.
Read the answer -
Python Beginner
What is the difference between loc and iloc, and when does each fail?
loc selects by label, iloc selects by position. They agree only while the index happens to be a clean range, which is exactly why the bug appears after you filter or sort.
Read the answer -
Python Beginner
What is the difference between loc and iloc in pandas?
loc selects by label (the index and column names), while iloc selects by integer position. They differ most when your index is not a simple 0-based range.
Read the answer -
Python Intermediate
What is the difference between map, replace and apply in pandas?
map substitutes values from a dictionary and turns anything unmapped into NaN, replace substitutes only the values you list and leaves the rest alone, and apply runs a function on every value.
Read the answer -
Python Advanced
What is the difference between nlargest on a groupby and sorting with head?
groupby.nlargest returns a Series with a MultiIndex of group and original position, which is awkward, while sort then groupby.head returns a normal frame. Use whichever output shape suits your next step.
Read the answer -
Python Advanced
What is the difference between plt and ax in matplotlib?
plt is the stateful interface that draws on whichever plot is current, while ax is an explicit Axes object you draw on directly. Use the object-oriented ax approach for anything beyond a quick single plot.
Read the answer -
Python Intermediate
What is the difference between re.match, re.search and re.findall in Python?
match only checks the start of the string, search scans anywhere for the first match, and findall returns every match as a list. Mixing up match and search is the usual source of confusion.
Read the answer -
Python Advanced
What is the difference between where, mask and clip in pandas?
where keeps values where the condition is True and replaces the rest, mask does the exact opposite, and clip caps values at a lower and upper bound. The inverted logic of where is the usual confusion.
Read the answer -
Python Advanced
When do I need to call copy() on a DataFrame?
Call copy() when you take a subset and intend to modify it independently of the original. Under Copy-on-Write in pandas 3.0 this is safer by default, but an explicit copy still makes your intent clear.
Read the answer -
Python Intermediate
When should I set a column as the index in pandas?
Set an index when you want fast label lookups, automatic alignment across frames, or time-series operations like resample. Otherwise leave the default, because an index adds confusion for little gain.
Read the answer -
Python Advanced
When should I use agg, transform, filter or apply on a groupby?
agg gives one row per group, transform gives one value per original row, filter keeps or drops whole groups, and apply is the slow fallback for anything else. Choosing by output shape resolves most groupby confusion.
Read the answer -
Python Intermediate
When should I use deque or Counter instead of a list or dict?
A deque adds and removes from both ends in constant time and can cap its own length, while a list is slow at the front. Counter tallies items and supports arithmetic between tallies.
Read the answer -
Python Advanced
When should I use pickle to save Python objects, and when should I not?
Pickle saves almost any Python object exactly, but it is unsafe to load from an untrusted source and is not a stable long-term format. Use it for your own temporary caches, and Parquet or JSON for data you must share or keep.
Read the answer -
Python Intermediate
Where do missing values go when I sort in pandas?
Missing values are placed last by default, regardless of sort direction. Use na_position="first" to move them to the top, and remember they are never compared, only positioned.
Read the answer -
Python Advanced
Which metric should I use to evaluate a classifier in Python?
Accuracy is misleading on imbalanced data. Choose precision when false alarms are costly, recall when misses are costly, F1 for a balance, and look at the confusion matrix to see what the model is really doing.
Read the answer -
Python Advanced
Why can I not assign a new value to a category column in pandas?
A category column only accepts values already in its category list, so assigning a new one raises. Add the category first with add_categories, or convert the column back to text.
Read the answer -
Python Intermediate
Why did my function change the list I passed into it?
Arguments are passed by reference, so a function that modifies a list or DataFrame changes the caller's object. Return a new object instead, or copy inside the function, so callers are not surprised.
Read the answer -
Python Intermediate
Why does -7 // 2 give -4 in Python?
Floor division rounds towards negative infinity, not towards zero, so a negative result rounds further down. Use int() on a true division if you want truncation towards zero instead.
Read the answer