Processing Large Datasets with Pandas

When working with large datasets, Pandas is one of the most powerful and flexible tools in Python. It provides data structures and functions that make it easy to read and process data from various formats such as CSV or Excel files.

Bernd Fischer

Bernd Fischer

14. Oct 2024 · 3 min read

Processing Large Datasets with Pandas

1. Installing Pandas

Before you start working with Pandas, the library needs to be installed. This is easily done with the following command:

pip install pandas

2. Reading CSV Files

CSV files are one of the most common formats for tabular data. With Pandas, you can efficiently read and manipulate CSV files.

Reading a Simple CSV File

Using the function pd.read_csv(), you can load a CSV file into a Pandas DataFrame.

import pandas as pd

# CSV-Datei einlesen
df = pd.read_csv('daten.csv')

# Daten anzeigen
print(df.head())

df.head() shows the first five rows of the DataFrame to get a quick preview of the data.

Selecting Columns and Specifying Data Types

If a CSV file is very large, it might be more efficient to read only the required columns and specify data types to reduce memory usage.

# Nur bestimmte Spalten einlesen
df = pd.read_csv('daten.csv', usecols=['Spalte1', 'Spalte2'], dtype={'Spalte1': 'int32', 'Spalte2': 'float32'})

print(df.head())
  • usecols: Selects only the specified columns from the file.
  • dtype: Sets the data types for the columns, which uses memory more efficiently.

Reading Data in Chunks

For very large CSV files, it can be useful to read the data in chunks to conserve memory. This is especially helpful if the file is too large to load completely into memory.

# Daten stückweise einlesen
for chunk in pd.read_csv('große_daten.csv', chunksize=10000):
    # Daten verarbeiten
    print(chunk.shape)

Using chunksize, Pandas reads the file in small parts. In this example, 10,000 rows are read and processed at a time.


3. Reading Excel Sheets

Besides CSV files, Pandas also supports reading Excel files using the function pd.read_excel().

Reading a Simple Excel File

# Excel-Datei einlesen
df = pd.read_excel('daten.xlsx')

# Daten anzeigen
print(df.head())

Pandas automatically detects the sheets in the Excel file and loads the first sheet unless another sheet is explicitly specified.

Reading a Specific Sheet from an Excel File

If your Excel file contains multiple sheets, you can specifically load one particular sheet:

# Bestimmtes Blatt einlesen
df = pd.read_excel('daten.xlsx', sheet_name='Blatt1')

print(df.head())

Reading Multiple Sheets at Once

Pandas also offers the possibility to read multiple sheets of an Excel file simultaneously and returns them as a dictionary:

# Alle Blätter einlesen
dfs = pd.read_excel('daten.xlsx', sheet_name=None)

# Daten aus mehreren Blättern anzeigen
for sheet_name, df in dfs.items():
    print(f"Blattname: {sheet_name}")
    print(df.head())

In this case, dfs is a dictionary where the sheet names are the keys and the DataFrames are the values.


4. Recommended Maximum Data Size for Pandas

Pandas is designed for processing moderate to large datasets, but there are limits where performance degrades. A recommended maximum size for working with Pandas is about 5 to 10 million rows or approximately 1 to 5 GB of data, depending on the complexity of the operations and the available system memory.

With larger data volumes, issues such as high memory usage, slow processing, or even crashes may occur. In such cases, it may be advisable to either split the data into smaller parts or use more efficient tools designed for big data.


5. Efficient Memory Management for Large Datasets

When working with large data volumes, memory consumption can quickly become an issue. Here are some tips to use memory efficiently:

Optimize Data Types

By specifying more efficient data types, e.g., using int32 or float32 instead of the default int64 or float64, memory usage can be significantly reduced.

df = pd.read_csv('daten.csv', dtype={'Spalte1': 'int32', 'Spalte2': 'float32'})

Avoid Unnecessary Columns

When reading in data, select only the columns you actually need to reduce memory consumption.

df = pd.read_csv('daten.csv', usecols=['WichtigeSpalte1', 'WichtigeSpalte2'])

Saving and Loading DataFrames

When working with large data, it can be practical to save data in Pandas' native format (Pickle) and reload it later faster.

# Speichern
df.to_pickle('daten.pkl')

# Laden
df = pd.read_pickle('daten.pkl')

6. Alternatives to Pandas for Large Datasets

When Pandas reaches its performance limits, there are several more powerful alternatives better suited for processing very large datasets.

Dask

Dask is a library that mimics the Pandas API but is designed for distributed and parallel data processing. It can split large DataFrames into smaller chunks and process them in parallel, making it suitable for large datasets and distributed environments.

import dask.dataframe as dd

# Dask-DataFrame einlesen
df = dd.read_csv('große_daten.csv')

# Berechnungen verzögert ausführen
df.head()

Dask offers an easy way to scale Pandas code when needed, as many of the same Pandas operations are supported.

Vaex

Vaex is another powerful library for working with large datasets. Vaex uses a memory-efficient method by processing data from disk without loading it fully into RAM.

import vaex

# Vaex-DataFrame einlesen
df = vaex.open('große_daten.csv')

# Schnelle Analyse
df.describe()

Vaex is especially good for exploratory data analysis and large volumes of data.

PySpark

Apache Spark is a distributed data processing engine, and PySpark is its Python interface. PySpark allows processing large data volumes across clusters by parallelizing and distributing data.

from pyspark.sql import SparkSession

# SparkSession starten
spark = SparkSession.builder.appName("GroßeDaten").getOrCreate()

# Spark DataFrame einlesen
df = spark.read.csv('große_daten.csv')

# Analyse
df.show()

PySpark is particularly useful for big data projects and distributed processing across multiple machines.