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
Are Python dictionaries and sets ordered?
Dictionaries preserve insertion order from Python 3.7 onwards, and this is guaranteed. Sets are unordered and their iteration order must never be relied upon, even when it looks stable.
Read the answer -
Python Intermediate
Do Python type hints actually do anything at runtime?
Type hints are not enforced when the code runs, so a wrong type does not raise on its own. They are for readers and for external checkers like mypy, which catch mismatches before you run the code.
Read the answer -
Python Intermediate
How do I add a row to a DataFrame in pandas?
Build a list of rows and construct the DataFrame once, because adding rows one at a time is very slow. The old append method was removed, so use pd.concat if you must combine frames.
Read the answer -
Python Intermediate
How do I add row and column totals to a pivot table in pandas?
Pass margins=True to pivot_table to add an All row and column holding the totals. Name it with margins_name, and use fill_value so empty cells show zero rather than NaN.
Read the answer -
Python Intermediate
How do I aggregate several columns with different functions in pandas?
Pass named aggregations to agg, giving each output column a name and the column-function pair that fills it. This produces flat, clearly named columns instead of a two-level header.
Read the answer -
Python Intermediate
How do I avoid KeyError when grouping items into a dictionary in Python?
Use collections.defaultdict with a factory like list or int so a missing key is created automatically on first access. This removes the check-then-create boilerplate that a plain dict needs.
Read the answer -
Python Intermediate
How do I build a dictionary with a comprehension in Python?
Use braces with a key-value expression and a for clause to build a dictionary in one line. It is clearer than a loop for simple transforms, and a common use is inverting a dictionary or filtering its items.
Read the answer -
Python Intermediate
How do I build a frequency table of two categories in pandas?
Use crosstab to count how often each combination of two categories occurs, laid out as a grid. Add normalize for proportions and margins for row and column totals.
Read the answer -
Python Intermediate
How do I calculate a cumulative average in pandas?
Use expanding, which grows the window from the start rather than keeping a fixed size, then apply mean or another aggregation. It answers the question of the average so far, at each point.
Read the answer -
Python Intermediate
How do I calculate the correlation between columns in pandas?
Use corr between two columns for a single coefficient, or on the whole frame for a matrix. It measures linear association only, ignores missing values pairwise, and does not imply causation.
Read the answer -
Python Intermediate
How do I check whether any or all rows meet a condition in pandas?
Call any or all on a boolean condition to reduce it to a single True or False. Use them for data validation checks, and remember they operate per column on a DataFrame unless you reduce twice.
Read the answer -
Python Intermediate
How do I check whether every column in a row meets a condition?
Apply the condition to the whole frame and reduce with all(axis=1) for a per-row answer, or all(axis=0) for a per-column one. The axis argument decides which direction is collapsed.
Read the answer -
Python Intermediate
How do I clean messy text values in a pandas column?
Chain the str accessor methods to strip whitespace and normalise case, and strip out stray characters before converting to numbers. Inconsistent whitespace and case are the usual cause of failed joins and duplicate groups.
Read the answer -
Python Intermediate
How do I combine NumPy arrays into one?
Use vstack to stack rows, hstack to join columns, and concatenate with an axis for the general case. The shapes must match on every axis except the one you are joining along.
Read the answer -
Python Intermediate
How do I compare a value to several rows back in pandas?
Pass a periods argument to diff or shift to look back more than one row, such as twelve for a year-on-year monthly comparison. The first rows come out as NaN because there is nothing that far back.
Read the answer -
Python Intermediate
How do I compare the columns or keys of two DataFrames?
A pandas Index supports set operations, so use intersection, difference and union to compare column names or key values. This is the quickest way to explain why a merge lost rows.
Read the answer -
Python Intermediate
How do I convert a column to a different type safely in pandas?
astype raises on any value it cannot convert, while to_numeric with errors="coerce" turns the bad ones into NaN so you can inspect them. Convert deliberately and check what failed.
Read the answer -
Python Intermediate
How do I convert a column to dates and extract the month or year in pandas?
Convert the column with to_datetime, then read parts through the dt accessor such as dt.year or dt.month. Set the format or dayfirst argument when parsing is ambiguous.
Read the answer -
Python Intermediate
How do I convert between strings and dates in Python with strptime and strftime?
strptime parses a string into a datetime using a format code, and strftime formats a datetime back into a string. The format codes must match the text exactly, and mixing the two directions is the usual mistake.
Read the answer -
Python Intermediate
How do I convert labels to numeric codes in pandas?
Use factorize to turn labels into integer codes plus the list of unique values, which lets you map back. Do not use the codes as a numeric feature unless the categories genuinely have an order.
Read the answer -
Python Intermediate
How do I create a column that depends on another column I just created in pandas?
Inside a single assign call, use a lambda for the second column so it sees the frame including the first. Referring to the original frame directly fails, because that column does not exist yet.
Read the answer -
Python Intermediate
How do I create a new column based on conditions in pandas?
Use np.where for a two-way choice and np.select for several conditions. Both are vectorised, so they are faster and cleaner than looping or using apply with an if statement.
Read the answer -
Python Intermediate
How do I create every combination of rows from two DataFrames in pandas?
Use merge with how="cross" to pair every row of one frame with every row of the other. The result grows as the product of the two sizes, so it can become very large quickly.
Read the answer -
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 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