"Large data" workflows using pandas
Here is an example of a workflow for handling large data using the pandas library:
Here is an example of a workflow for handling large data using the pandas library:
Python: Large data workflows using Pandas
import pandas as pd
# Read in large data file using the chunksize parameter to read in chunks
# instead of loading the entire file into memory
df_iterator = pd.read_csv("large_data.csv", chunksize=100000)
# Process each chunk of data and write directly to disk to save memory
for i, df_chunk in enumerate(df_iterator):
# Perform data cleaning and preprocessing on chunk
df_chunk = df_chunk.dropna()
df_chunk["column_name"] = df_chunk["column_name"].str.lower()
# Write chunk to CSV. Use mode='w' for the first chunk, mode='a' for subsequent ones
mode = 'w' if i == 0 else 'a'
header = i == 0
df_chunk.to_csv("cleaned_large_data.csv", mode=mode, header=header, index=False)
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
In this example, the large data file is read in using the pd.read_csv() function with the chunksize parameter set to 100000. This reads in the file in chunks of 100000 rows, allowing for the data to be processed in smaller chunks rather than loading the entire file into memory. Each chunk is then cleaned and preprocessed, and written directly to disk. Using mode='w' for the first chunk and mode='a' (append) for subsequent chunks builds the output file incrementally. This avoids the memory limitation of concatenating all chunks back into a single DataFrame, which would defeat the purpose of chunking for large datasets.