Hugging Face, End to EndLesson 41 of 75

Module 5 · Datasets, and the work that happens before any model

Transforming a million rows without waiting an hour

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 4 of 10 in this chapter

This chapter’s goalInspect, stream, filter and transform a dataset far larger than your RAM, query the Hub’s parquet conversion with SQL before downloading anything, run duplicate, leakage and class-balance checks on your own data, and publish it with a card stating provenance and licence.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:05Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

The naive version, and the fast one

Python
# slow: one Python call per row
ds = ds.map(lambda r: {"n_chars": len(r["text"])})

# fast: one Python call per thousand rows
ds = ds.map(
    lambda b: {"n_chars": [len(t) for t in b["text"]]},
    batched=True, batch_size=1000,
)

In batched mode the function receives a dictionary of input lists. All output lists in a batch must have the same length as one another. That output length can differ from the input length. Batching can reduce call overhead. Benchmark it with your own data and transformation.

Batching can also help fast tokenizers process several strings together. Compare results on your own data:

Python
def tok_fn(batch):
    return tok(batch["text"], truncation=True, max_length=512)

ds = ds.map(tok_fn, batched=True, remove_columns=["text"])

remove_columns drops the original text after tokenization in this example. Compare the resulting cache sizes on your data before budgeting storage.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Transforming a million rows without waiting an hour’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/map-filter-and-batching (Accessed: 29 November 2026).