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 Advanced

    How do I write multiple sheets to one Excel file with pandas?

    Use pd.ExcelWriter as a context manager and call to_excel once per sheet with a different sheet_name. Writing each frame to a separate file, or reopening the writer, are the mistakes that lose sheets.

    Read the answer
  2. Python Advanced

    How do I write my own context manager in Python?

    Write a generator decorated with contextlib.contextmanager, putting setup before yield and cleanup after it. The cleanup runs even if the block raises, so it is the reliable place for teardown.

    Read the answer
  3. Python Advanced

    How do I write reusable transformation steps for pandas pipelines?

    Write functions that take a DataFrame and return a DataFrame, then slot them into a chain with pipe. This makes each step testable and reusable, instead of a long script of one-off mutations.

    Read the answer
  4. Python Advanced

    How do I write tests with pytest in Python?

    Write functions named test_ that use plain assert, and run pytest to discover and run them. Use fixtures for shared setup and parametrize to run one test over many inputs.

    Read the answer
  5. Python Beginner

    How do list comprehensions work and when should I use one?

    A list comprehension builds a list in one expression combining a loop and an optional condition. Use it for simple transform-and-filter tasks, but fall back to a normal loop when the logic gets complicated.

    Read the answer
  6. Python Advanced

    How do query and eval speed up work on large DataFrames in pandas?

    query filters and eval computes using a string expression that pandas can evaluate without building large intermediate arrays. On big frames this saves memory and can be faster, though on small frames the overhead is not worth it.

    Read the answer
  7. Python Intermediate

    Should I use inplace=True in pandas?

    Generally no. inplace rarely saves memory, it breaks method chaining, and it returns None, which causes bugs when people assign the result. Reassigning the returned frame is clearer and safer.

    Read the answer
  8. Python Advanced

    Should I use Parquet instead of CSV in pandas?

    Parquet stores data in a compressed columnar format that keeps dtypes and lets you read only the columns you need, so it is smaller and faster than CSV. Use CSV only when a human or another tool must read the file.

    Read the answer
  9. Python Advanced

    Should I use Polars instead of pandas?

    Polars is a faster DataFrame library with a lazy query engine and no index, which suits large data and long pipelines. Pandas has a far larger ecosystem, so choose Polars for speed and pandas for compatibility.

    Read the answer
  10. Python Advanced

    Should I use statsmodels or scikit-learn for regression?

    Use statsmodels when you want to explain relationships, because it gives p-values, confidence intervals and diagnostics. Use scikit-learn when you want to predict, because it gives pipelines, cross-validation and tuning.

    Read the answer
  11. Python Advanced

    What are argsort and searchsorted in NumPy for?

    argsort returns the positions that would sort an array, which lets you reorder several arrays consistently. searchsorted finds where a value would be inserted in a sorted array, which is a fast way to bucket values.

    Read the answer
  12. Python Intermediate

    What do *args and **kwargs mean in Python?

    args collects extra positional arguments into a tuple, and kwargs collects extra keyword arguments into a dictionary. The same star syntax also unpacks a sequence or dictionary back into arguments at the call site.

    Read the answer
  13. Python Intermediate

    What does -1 mean in NumPy reshape?

    Passing -1 as a dimension tells NumPy to work out that size itself from the total number of elements. It is a convenience so you do not have to compute the length by hand, and only one dimension may be -1.

    Read the answer
  14. Python Beginner

    What does if __name__ == "__main__" do in Python?

    It runs a block only when the file is executed directly, not when it is imported. This lets a file work both as a runnable script and as an importable module without side effects.

    Read the answer
  15. Python Intermediate

    What does reset_index do in pandas and when do I need drop=True?

    reset_index moves the index back into a column and renumbers the rows. Pass drop=True when the old index is meaningless, or you get an unwanted extra column of stale labels.

    Read the answer
  16. Python Intermediate

    What does the axis argument mean in NumPy and pandas?

    axis names the dimension the operation moves along and collapses. axis=0 runs down the rows to give a result per column, and axis=1 runs across the columns to give a result per row.

    Read the answer
  17. Python Intermediate

    What does the with statement (context manager) do in Python?

    The with statement guarantees setup and cleanup around a block, most often closing a file even if an error occurs. It replaces manual try and finally for managing resources.

    Read the answer
  18. Python Advanced

    What do stack and unstack do in pandas?

    stack moves column labels down into the index, making the frame longer and narrower, and unstack does the reverse. They reshape around the index, unlike melt and pivot which work on columns.

    Read the answer
  19. Python Advanced

    What do the different rank methods mean in pandas?

    The method argument decides how ties are ranked. min and dense both give tied rows the same rank but differ on the gap after them, first breaks ties by order, and average splits the rank.

    Read the answer
  20. Python Advanced

    What happens when I add a month to the 31st of January?

    Adding a month to a date that does not exist in the target month clamps to the last valid day, so 31 January becomes 28 February. Use MonthEnd if you actually mean the end of the month.

    Read the answer
  21. Python Intermediate

    What is a generator in Python and when should I use one?

    A generator produces values one at a time on demand using yield, instead of building a whole list in memory. Use one for large or streaming data, or when you may not consume every value.

    Read the answer
  22. Python Intermediate

    What is a namedtuple in Python and when should I use one?

    A namedtuple is a lightweight immutable record whose fields have names, so you access them by attribute instead of position. Use it for small fixed records where a full class would be overkill.

    Read the answer
  23. Python Beginner

    What is a Python virtual environment and why should I use one?

    A virtual environment is an isolated set of packages for one project, so different projects can use different versions without clashing. Create one with the venv module and activate it before installing anything.

    Read the answer
  24. Python Advanced

    What is a sparse column in pandas and when is it worth using?

    A sparse column stores only the non-default values, saving memory when most entries are the same. It helps for very sparse data like one-hot encodings, but adds overhead and complexity otherwise.

    Read the answer
  25. Python Intermediate

    What is broadcasting in NumPy?

    Broadcasting lets NumPy apply an operation between arrays of different shapes by stretching the smaller one across the larger, without copying data. It powers concise, fast array maths but follows strict shape rules.

    Read the answer