</>
Skip to content
Pandas lessons (42/42)

Pandas — Memory Optimization

Check memory usage

import pandas as pd

df = pd.read_csv('large.csv')
print(df.info(memory_usage='deep'))
print(df.memory_usage(deep=True).sum() / 1024**2, "MB")

Optimize memory

# Reduce numeric types
for col in df.select_dtypes(include=['int']).columns:
    df[col] = pd.to_numeric(df[col], downcast='integer')

for col in df.select_dtypes(include=['float']).columns:
    df[col] = pd.to_numeric(df[col], downcast='float')

# Convert strings to categories
for col in df.select_dtypes(include=['object']).columns:
    if df[col].nunique() < len(df) * 0.5:
        df[col] = df[col].astype('category')

Read in chunks

chunks = pd.read_csv('large.csv', chunksize=100000)
processed = pd.concat([chunk.process() for chunk in chunks])

Mini Practice

  1. Check memory usage
  2. Optimize dtypes
  3. Use categories
  4. Process in chunks

Up Next

Continue with Pipes - Method chaining.

Related Topics

Frequently Asked Questions about Memory Optimization

What is Memory Optimization in Pandas?

Memory Optimization is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Memory Optimization?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Memory Optimization.

Why is Memory Optimization important in Pandas?

Memory Optimization is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.