Start learning free

LESSON 7 OF 107 · DATA ENGINEERING: FILES AND FORMATS

Compression and Size in Python

Practice

Compression stores the same data in fewer bytes. Nothing is lost: when you read the file back, you get exactly the values you wrote. Parquet compresses each column on its own, and you pick how with compression= in to_parquet: "snappy" is fast and is what pandas uses if you don't say, while "zstd" (Zstandard) works a little harder and usually gives a smaller file.

Columns compress well because each column's values sit together. A column like status holds paid or cancelled on every row, so it is the same few words again and again, and repeats are what compression feeds on. A CSV spells out every value of every row as text. This matters at work: storage is paid by the byte, and every query has to move those bytes off a disk or over a network.

os.path.getsize(path) gives a file's size in bytes; divide by 1024 for KB. For example, with the customers table:

customers = pd.read_csv("customers.csv")
customers.to_parquet("customers.parquet", index=False, compression="zstd")
print(round(os.path.getsize("customers.parquet") / 1024), "KB")

Your program prints each of the three files with its size. Then the check names the smallest file, says how many times as big the CSV is, and lists the zstd file's columns from smallest to biggest:

orders.csv 64 KB
orders_snappy.parquet 17 KB
orders_zstd.parquet 14 KB
smallest: orders_zstd.parquet
the CSV is 4.7 times as big
columns in the zstd file, smallest first: status, category, quantity, unit_price, product, customer_id, date, order_id

Your task: below orders, write it to orders_snappy.parquet with snappy and to orders_zstd.parquet with zstd, then loop over the three file names and print each one with its size in KB.


Assignment
Follow the Coding Tutorial to save the orders twice as Parquet, with two compressions, and print the size of all three files.


Hint
Both writes take index=False; only the file name and the compression= value change. os.path.getsize gives bytes, so divide by 1024 and wrap it in round to get whole KB.


STEP 1 OF 4
    Ctrl + Enter to check

    De Compression Size in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free