Loading tutorials
pd.read_csv. The machine may have more free RAM than the file's size and still run out. This chapter explains why. It also gives you a way to judge, before you load anything, whether a file is likely to fit. The rest of the tutorial 7 min readpd.read_csv, you know what a DataFrame and a dtype are, and you are comfortable with basic Python loops. You will learn how to measure what a DataFrame costs in memory, why the number pandas gives you can be wrong, and how to use a small sample to plan 6 min readwith blocks), pandas DataFrames and read_csv, and simple groupby aggregation. Earlier chapters explained why a big file can exhaust memory and how to measure a DataFrame's size. Here you learn the core tool for working around that limit: th6 min readpd.read_csv(..., chunksize=...) returns. It assumes you know basic Python loops and context managers, and that you have used read_csv and DataFrames. The earlier chapter showed a plain for loop over the chunks. Here you will see what that object is, how to u5 min readpd.read_csv, know what a DataFrame's dtypes are, and have seen chunksize iteration. Chunking limits how many rows you hold at once. usecols and dtype limit how much each row costs. Combined, they often decide whether a job fits in memory at all.5 min readpd.read_csv(..., chunksize=...), use a with block on the reader, and filter a DataFrame with a boolean mask. Here you will use those pieces to pull a small subset out of a file that is too large to load whole.6 min readpd.concat at the end, the result must fit in memory. That brings back the problem chunking was meant to avoid. This chapter shows how to append each chunk's o6 min readfor loop and a with block, that you know pandas DataFrames and read_csv, and that you have used groupby with agg. It also assumes you already know how to read a CSV in chunks with chunksize. Here the question is how to compute a statistic when 5 min readgroupby and aggregation, you can iterate over a read_csv reader with chunksize, and you have seen the pattern of keeping a running total across chunks. Here we extend that pattern to grouped results, where every chunk produces its own small table of p6 min readpd.read_csv(..., chunksize=...), know what usecols and dtype do, and have used memory_usage(deep=True). Here you will learn how to pick a chunksize value on purpose instead of copying 100_000 from an example.6 min readchunksize, loop over the reader, and aggregate with groupby. It covers three mistakes that produce wrong numbers or memory errors in chunked code. Two of them fail silently.6 min readchunksize, combine partial results across chunks, and write output incrementally. The same ideas apply to other sources. What changes is the function you call and the options it offers. You will see how to read in pieces from a SQL dat6 min readread_csv with chunksize, per-chunk filtering, and combining partial aggregates. It also assumes you know basic pandas groupby. You don't need prior experience with the three tools below. The goal is to help you deci6 min read