Python · medium

You need to process a 50GB CSV on a machine with 16GB of RAM. How?

Asked in Data Engineer interviews, in the Python round.

Short answer

Stream it instead of loading it: read_csv with chunksize, or hand it to an engine that spills to disk and reads only the columns you touch (DuckDB, Polars lazy, Spark). If it is a recurring job, convert once to Parquet — columnar plus compression usually cuts both IO and memory by an order of magnitude. The general move is to make the working set fit in memory, not the file.

How to answer it

Do not load it. Make the working set fit, not the file.

The three moves, from least to most change:

import duckdb
duckdb.sql('''
  SELECT region, SUM(amount) AS revenue
  FROM read_csv_auto('big.csv')
  GROUP BY region
''').df()

Spark is the answer when the data is on a cluster already; it is not the answer for one file on one machine.

Related questions

Practice this for real