Q&A library

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.

  1. Python Beginner

    How do I add a prefix or suffix to every column name in pandas?

    Use add_prefix or add_suffix to tag every column at once, which is useful before merging two frames with overlapping names. Use rename for selective changes instead.

    Read the answer
  2. Python Beginner

    How do I apply a function to a column in pandas?

    Use map or apply on a single column to run a function on every value, but prefer a vectorised expression or built-in string and numeric methods where one exists, because they are much faster.

    Read the answer
  3. Python Beginner

    How do I calculate each row's percentage of the total in pandas?

    Divide the column by its own sum for a share of the overall total, or by a group total from transform for a share within each group. Multiply by 100 for a percentage.

    Read the answer
  4. Python Beginner

    How do I check the type of a variable in Python?

    Use isinstance, which respects inheritance and accepts a tuple of types, rather than comparing type() directly. Be aware that bool is a subclass of int, so a True value passes an int check.

    Read the answer
  5. Python Beginner

    How do I check whether a file exists in Python?

    Use Path.exists from pathlib, or os.path.exists. But for the common case of opening a file, it is often better to just try and handle the error, to avoid a race condition.

    Read the answer
  6. Python Beginner

    How do I convert a string to a number in Python?

    Use int for whole numbers and float for decimals. int rejects a string with a decimal point, so convert to float first, and wrap the conversion in try/except to handle input that is not a number.

    Read the answer
  7. Python Beginner

    How do I count distinct values in pandas?

    Use nunique for the number of distinct values and unique to list them. nunique ignores missing values by default, and works per group when combined with groupby.

    Read the answer
  8. Python Beginner

    How do I count how often each value appears in a column?

    Use value_counts on the column for a quick frequency table sorted by count. Pass normalize for proportions, and dropna to include missing values.

    Read the answer
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. Python Beginner

    How do I handle missing values (NaN) in pandas?

    Detect missing values with isna, remove them with dropna, or replace them with fillna. Decide deliberately whether dropping or filling is correct, because each changes your results differently.

    Read the answer
  15. Python Beginner

    How do I insert a column at a specific position in pandas?

    Use df.insert with the position, name and values, which puts the column exactly where you want rather than at the end. Reordering with a column list is the alternative for bigger rearrangements.

    Read the answer
  16. Python Beginner

    How do I loop over a dictionary's keys and values in Python?

    Loop with .items() to get keys and values together, .keys() for keys, or .values() for values. Iterating the dictionary directly gives keys, which trips up people expecting pairs.

    Read the answer
  17. Python Beginner

    How do I loop over two lists at once in Python?

    Use zip to pair the items from two sequences and loop over the pairs. Note that zip stops at the shorter one, silently ignoring extra items, so pass strict=True to catch a length mismatch.

    Read the answer
  18. Python Beginner

    How do I loop with an index in Python without using range(len())?

    Use enumerate to get the index and the item together, instead of looping over range(len(x)) and indexing back in. It is clearer, less error prone, and works on any iterable.

    Read the answer
  19. Python Beginner

    How do I merge two dictionaries in Python?

    On Python 3.9 and later use the pipe operator, or double-star unpacking on older versions. When keys collide, the value from the right-hand dictionary wins.

    Read the answer
  20. Python Beginner

    How do I quickly understand an unfamiliar DataFrame in pandas?

    Run info for the shape, dtypes and missing counts, describe for the numeric distribution, and nunique to spot identifiers and constants. These three checks catch most data problems before you start analysing.

    Read the answer
  21. Python Beginner

    How do I read a CSV file with pandas and avoid the common problems?

    Use pd.read_csv, but set dtype, parse_dates and the separator explicitly when the defaults guess wrong. The usual pain points are leading zeros stripped from codes and dates left as text.

    Read the answer
  22. Python Beginner

    How do I read a file line by line in Python?

    Open the file in a with block and iterate over it directly, which reads one line at a time and is memory efficient. Strip the trailing newline from each line if you do not want it.

    Read the answer
  23. Python Beginner

    How do I remove duplicates from a list while keeping the order?

    Use dict.fromkeys to drop duplicates while preserving first-seen order. Converting to a set removes duplicates too but loses order, which is the common mistake.

    Read the answer
  24. Python Beginner

    How do I rename columns in a pandas DataFrame?

    Use rename with a dictionary to change named columns, or assign a full list to df.columns to replace them all. rename returns a new frame unless you reassign or pass inplace.

    Read the answer
  25. Python Beginner

    How do I rename index labels in pandas?

    Use rename with a dictionary to change specific index labels, and rename_axis to change the name of the index itself. The two are easy to confuse and do different things.

    Read the answer