CSV
TensorFlow
Data Processing
Machine Learning
Python

Read in Large CSV File and feed into TensorFlow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If the CSV file is large, the wrong approach is to load the whole thing into pandas and then convert it into TensorFlow tensors. That works for small experiments but quickly becomes memory-bound and slow. The scalable pattern is to stream the data through tf.data, parse rows lazily, batch them, and prefetch so model training overlaps with input work.

The Goal: Stream Instead of Materialize Everything

A large CSV may be gigabytes in size. If you read it all into memory first, you pay for:

  • the raw text file
  • the parsed DataFrame
  • the converted NumPy arrays
  • the final TensorFlow tensors

That is unnecessary for training. TensorFlow is designed to consume data incrementally.

Use make_csv_dataset for Tabular Data

TensorFlow provides a high-level helper for CSV input pipelines.

python
1import tensorflow as tf
2
3dataset = tf.data.experimental.make_csv_dataset(
4    file_pattern="train.csv",
5    batch_size=32,
6    label_name="target",
7    num_epochs=1,
8    shuffle=True,
9    ignore_errors=True,
10)
11
12for features, labels in dataset.take(1):
13    print(features)
14    print(labels)

This reads the file in a streaming fashion, parses columns automatically, and yields batches ready for model training.

For many tabular training jobs, this is the simplest correct answer.

Control Column Types Explicitly When Needed

If type inference is not reliable, declare the CSV schema yourself.

python
1import tensorflow as tf
2
3column_defaults = [tf.float32, tf.float32, tf.int32]
4
5dataset = tf.data.experimental.make_csv_dataset(
6    file_pattern="train.csv",
7    batch_size=16,
8    column_names=["feature_a", "feature_b", "target"],
9    column_defaults=column_defaults,
10    label_name="target",
11    header=True,
12)

This avoids parsing surprises and makes the training pipeline more stable.

Lower-Level Control with TextLineDataset

If the file format is unusual or you need custom parsing, build the pipeline yourself.

python
1import tensorflow as tf
2
3def parse_line(line):
4    defaults = [0.0, 0.0, 0]
5    values = tf.io.decode_csv(line, record_defaults=defaults)
6    features = tf.stack(values[:2])
7    label = values[2]
8    return features, label
9
10dataset = (
11    tf.data.TextLineDataset("train.csv")
12    .skip(1)
13    .map(parse_line, num_parallel_calls=tf.data.AUTOTUNE)
14    .batch(32)
15    .prefetch(tf.data.AUTOTUNE)
16)
17
18for batch_features, batch_labels in dataset.take(1):
19    print(batch_features.shape)
20    print(batch_labels.shape)

This is more verbose, but it gives full control over parsing, feature engineering, and error handling.

Batch, Shuffle, and Prefetch Correctly

The input pipeline matters almost as much as the model for training throughput.

A typical order is:

  1. read the file
  2. parse rows
  3. shuffle
  4. batch
  5. prefetch

Example:

python
1dataset = (
2    tf.data.TextLineDataset("train.csv")
3    .skip(1)
4    .map(parse_line, num_parallel_calls=tf.data.AUTOTUNE)
5    .shuffle(10000)
6    .batch(64)
7    .prefetch(tf.data.AUTOTUNE)
8)

prefetch allows TensorFlow to prepare the next batch while the current batch is being used by the model.

Feeding the Dataset into a Model

Once the pipeline yields batches of features and labels, use it directly in model.fit.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(2,)),
5    tf.keras.layers.Dense(16, activation="relu"),
6    tf.keras.layers.Dense(1)
7])
8
9model.compile(optimizer="adam", loss="mse")
10model.fit(dataset, epochs=3)

This avoids the need to first convert the whole CSV into one giant tensor.

When Pandas Still Makes Sense

Pandas is still useful for:

  • inspecting a sample of the file
  • understanding column names and missing values
  • prototyping transformations on a small subset

For example:

python
1import pandas as pd
2
3sample = pd.read_csv("train.csv", nrows=5)
4print(sample.head())

But pandas should not be the default ingestion path for truly large training data unless you have already confirmed the whole dataset fits comfortably in memory.

Consider TFRecord for Repeated Training

CSV is easy to inspect but inefficient for repeated large-scale training. If the same dataset is used many times, preprocessing once into TFRecord can make the pipeline faster and more consistent. CSV is fine to start with, but it is not always the best long-term storage format for TensorFlow training.

That is a performance optimization, not a requirement. The main first step is to stop materializing giant CSVs into memory.

Common Pitfalls

The biggest mistake is reading a very large CSV fully into pandas and only then trying to hand it to TensorFlow. Another is skipping batching and prefetching, which starves the model during training. Developers also often rely on automatic type inference and then get unstable parsing behavior when the CSV contains mixed or missing values. Finally, building a tf.data pipeline but still converting everything to NumPy first defeats most of the benefit.

Summary

  • For large CSV files, stream data through tf.data instead of loading everything eagerly.
  • 'tf.data.experimental.make_csv_dataset is the simplest high-level pipeline for many tabular cases.'
  • Use TextLineDataset plus decode_csv when you need custom parsing.
  • Batch, shuffle, and prefetch to keep training throughput healthy.
  • Use pandas only for inspection or small experiments, not as the default ingestion path for very large training data.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.