Python · medium
Asked in Data Engineer interviews, in the Python round.
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.
Do not load it. Make the working set fit, not the file.
The three moves, from least to most change:
pd.read_csv(path, chunksize=1_000_000, usecols=[...], dtype={...}) and aggregate chunk by chunk. Reading only the columns you touch and giving pandas real dtypes (category for low-cardinality strings, int32 where it fits) often cuts memory by five to ten times on its own.SELECT region, SUM(amount) FROM read_csv_auto('big.csv') GROUP BY 1 runs on a laptop.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.