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 make a category column sort in a meaningful order in pandas?

    Convert to an ordered category with the levels listed in the order you want, so sorting and comparison follow your order rather than the alphabet. Note that pulling out a single value returns a plain string again.

    Read the answer
  2. Python Intermediate

    How do I make an HTTP request in Python with requests?

    Use requests.get with a params dict for the query string, then check status_code and call json to parse the body. Set a timeout, and raise for status so failed requests do not pass silently.

    Read the answer
  3. Python Advanced

    How do I make sure my Python project runs the same way next year?

    Pin exact versions in a lock file, not just loose ranges, so an upgrade to a dependency cannot silently change your results. Record the Python version too, and rebuild the environment from the file.

    Read the answer
  4. Python Advanced

    How do I measure which part of my Python code is slow?

    Use timeit to compare small snippets accurately and cProfile to find which functions dominate a whole program. Measure before optimising, because the slow part is often not the one you expect.

    Read the answer
  5. 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
  6. Python Advanced

    How do I number the rows within each group in pandas?

    Use groupby with cumcount to give each row a position within its group, starting at zero. Sort first so the numbering follows a meaningful order, and add one if you want it to start at one.

    Read the answer
  7. Python Advanced

    How do I parse dates that come in several different formats?

    Use to_datetime with errors="coerce" so unparseable values become NaT rather than stopping the run, then inspect what failed. Set format or dayfirst explicitly, because guessing is slow and ambiguous.

    Read the answer
  8. Python Advanced

    How do I parse ISO timestamps with time zone offsets in pandas?

    Pass utc=True to to_datetime so mixed offsets are normalised to a single timezone-aware column. Without it, mixed offsets produce an object column of separate timestamps that behaves badly.

    Read the answer
  9. Python Intermediate

    How do I plot straight from a pandas DataFrame?

    Call .plot() on a DataFrame or Series, choosing the chart with the kind argument. It uses matplotlib underneath and returns an Axes, so you can fine-tune the result with matplotlib.

    Read the answer
  10. Python Advanced

    How do I plot with seaborn and how does it relate to matplotlib?

    Seaborn is a layer on top of matplotlib that plots directly from a DataFrame with far less code. It returns matplotlib objects, so you use matplotlib to fine-tune titles, limits and saving.

    Read the answer
  11. Python Advanced

    How do I process a large file without loading it into memory in Python?

    Iterate the file line by line and chain generators so only one item is in memory at a time. Building a list of every line, or calling readlines, defeats the purpose.

    Read the answer
  12. 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
  13. 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
  14. Python Advanced

    How do I read a CSV that is too big to fit in memory with pandas?

    Pass chunksize to read_csv to iterate over the file in pieces, aggregating as you go so the whole file never sits in memory at once. Combine partial results at the end rather than concatenating every chunk.

    Read the answer
  15. Python Intermediate

    How do I read a CSV where numbers use commas as separators?

    Pass thousands="," for grouped digits, and decimal="," with the right sep for European files. Without them, numbers arrive as text and every later calculation silently fails or gives the wrong answer.

    Read the answer
  16. 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
  17. Python Intermediate

    How do I read and write JSON in Python?

    Use json.load and json.dump for files, and json.loads and json.dumps for strings. The s versions work with strings, and common traps are non-serialisable types and forgetting that keys become strings.

    Read the answer
  18. Python Intermediate

    How do I read an Excel file with pandas?

    Use pd.read_excel, choosing the sheet with sheet_name, and pass sheet_name=None to load every sheet as a dictionary of DataFrames. You need an engine like openpyxl installed for xlsx files.

    Read the answer
  19. Python Advanced

    How do I read from and write to a SQL database with pandas?

    Use read_sql to run a query into a DataFrame and to_sql to write one back. Push aggregation into the SQL where you can, and always use parameters rather than formatting values into the query string.

    Read the answer
  20. 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
  21. 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
  22. 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
  23. Python Advanced

    How do I reorder or rename the levels of a MultiIndex in pandas?

    Use swaplevel to change the level order and rename_axis to rename them, then sort the index again. Forgetting to re-sort after swapping is what makes later selections slow or raise.

    Read the answer
  24. Python Advanced

    How do I reshape columns like sales_2020 and sales_2021 into rows in pandas?

    Use wide_to_long when several columns share a stub name and differ only by a suffix, such as a year. It unpivots those grouped columns into a value column plus a column holding the suffix.

    Read the answer
  25. Python Intermediate

    How do I reshape data between wide and long in pandas?

    Use pivot_table to turn long data into wide, and melt to turn wide data back into long. Long format is usually easier to group and plot, so reshape to it before analysis.

    Read the answer